python - 在 Python 中使用带有授权 HTTP header 的 GET 和 POST

标签 python http http-headers google-api

我正在尝试获取我在 Google map 中创建的 map 列表, map API 显示如下:


检索 map 列表

Maps Data API 提供了一个提要,其中列出了特定用户创建的 map ;该提要称为“元提要”。典型的 Maps Data API 元供稿是以下形式的 GET 请求:

默认提要请求与经过身份验证的用户关联的所有 map

GET http://maps.google.com/maps/feeds/maps/default/full
Authorization: GoogleLogin auth="authorization_token"

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

GET http://maps.google.com/maps/feeds/maps/userID/full
Authorization: GoogleLogin auth="authorization_token"

请注意,这两个 GET 请求都需要授权 HTTP header ,传递 AuthSub 或 GoogleLogin token ,具体取决于您实现的身份验证方案。 (GoogleLogin token 对应于 ClientLogin 身份验证过程。)


我不知道如何使用授权 HTTP header 创建 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]

我也看了httplib的文档,但没看懂(我不是专业程序员)。

有什么线索吗?

最佳答案

使用 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()

顺便说一句,Google map 数据 API 不是已弃用吗? http://googlegeodevelopers.blogspot.com/2010/11/maps-data-api-deprecation-announcement.html

关于python - 在 Python 中使用带有授权 HTTP header 的 GET 和 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7301593/

相关文章:

asp.net - 如何在 ASP.NET 请求中使用 System.Net.HttpRequestHeader 枚举?

python - 气泡图加权二维散点图数据到网格/网格

python - 从 python 代码控制本地 redis 服务器

node.js - 如何处理来自分布式系统的两个http请求?

PHP - HTTP header 中的文件名 : Problem with whitespaces

java - 使用 Java 的 Net 库时如何伪造特定的浏览器客户端?

ajax - AJAX 生成的 HTTP 请求消息的格式是什么?

python - 计算 pandas 中列组合的总和,按行计算,输出文件具有所述组合的名称

Python:解决 Python 在线编译器中的内存约束 p‌r‌o‌b‌l‌e‌m?

.net - 头与 WebClient?