在Python中使用GET和POST以及Authorization HTTP标头

hel*_*ker 5 python http google-api http-headers

我想在Google地图中获取由我创建的地图列表,而Maps API会说明以下内容:


检索地图列表

Maps Data API提供了一个列出特定用户创建的地图的Feed; 这种饲料被称为"metafeed".典型的Maps Data API元供稿是以下形式的GET请求:

默认源请求与经过身份验证的用户关联的所有映射

GET http://maps.google.com/maps/feeds/maps/default/full
Authorization: GoogleLogin auth="authorization_token"
Run Code Online (Sandbox Code Playgroud)

标准元供稿请求与关联的用户ID关联的所有映射

GET http://maps.google.com/maps/feeds/maps/userID/full
Authorization: GoogleLogin auth="authorization_token"
Run Code Online (Sandbox Code Playgroud)

请注意,两个GET请求都需要Authorization HTTP标头,并传递AuthSub或GoogleLogin标记,具体取决于您实施的身份验证方案.(GoogleLogin令牌对应于ClientLogin身份验证过程.)


我不知道如何使用Authorization HTTP标头创建HTTP请求.我已经有了代码来获取authorization_token,如下所示:

# coding: utf-8

import urllib, re, getpass

# http://code.google.com/intl/pt-BR/apis/maps/documentation/mapsdata/developers_guide_protocol.html#ClientLogin

username = 'heltonbiker'
senha = getpass.getpass('Senha do usuário ' + username + ':')

dic = {
        'accountType':      'GOOGLE',
        'Email':            (username + '@gmail.com'),
        'Passwd':           senha,
        'service':          'local',
        'source':           'helton-mapper-1'
        }
url = 'https://www.google.com/accounts/ClientLogin?' + urllib.urlencode(dic)
output = urllib.urlopen(url).read()
authid = output.strip().split('\n')[-1].split('=')[-1]
Run Code Online (Sandbox Code Playgroud)

我也看了一下httplibdocs,但不太懂(我不是专业的程序员).

任何线索?

Cla*_*ino 8

使用urllib2将使一切变得更容易:

import urllib2

request = urllib2.Request('http://maps.google.com/maps/feeds/maps/default/full')
request.add_header('Authorization', 'GoogleLogin auth=%s' % authorization_token)
urllib2.urlopen(request).read()
Run Code Online (Sandbox Code Playgroud)

顺便说一下,不推荐使用Google Maps Data API吗?http://googlegeodevelopers.blogspot.com/2010/11/maps-data-api-deprecation-announcement.html