IAM尝试在Python中使用OAuth2(代码与http://code.google.com/p/google-api-ads-python/source/browse/trunk/examples/adspygoogle/adwords/v201302/misc/ use_oauth2.py?spec=svn139&r=139):flow = OAuth2WebServerFlow(client_id='XXX', client_secret='YYY', scope='https://adwords.google.com/api/adwords', user_agent='ZZZ')authorize_url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob') code = raw_input('Code: ').strip() credential = None try: credential = flow.step2_exchange(code) #<- error except FlowExchangeError, e: sys.exit('Authentication has failed: %s' % e)这将在step2_exchange上生成“ socket.error:[Errno 10054]”错误,Python会切断该actact消息。因此,在用OAuthplayground检查密钥(以获得更好的errormsg)之后,我得到了以下错误:HTTP/1.1 400 Bad RequestContent-length: 37X-xss-protection: 1; mode=blockX-content-type-options: nosniffX-google-cache-control: remote-fetch-content-encoding: gzipServer: GSEVia: HTTP/1.1 GWAPragma: no-cacheCache-control: no-cache, no-store, max-age=0, must-revalidateDate: Thu, 06 Jun 2013 13:54:29 GMTX-frame-options: SAMEORIGINContent-type: application/jsonExpires: Fri, 01 Jan 1990 00:00:00 GMT{ "error" : "unauthorized_client"}我检查了client_id(对于已安装的应用程序)和client_secret是否与Google API控制台(https://code.google.com/apis/console/)中指定的相同。如果我通过OAuthPlayground进行整个过程,它将可以正常工作,但是如果我尝试使用游乐场创建的令牌,则该应用程序也会失败。有人知道如何解决吗?
2 回答
jeck猫
TA贡献1909条经验 获得超7个赞
https://code.google.com/p/httplib2/wiki/Examples中提供了一个有关如何在httplib2中配置proxy_info的示例。
上面写着:
import httplib2
import socks
httplib2.debuglevel=4
h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localhost', 8000))
r,c = h.request("http://bitworking.org/news/")
但是,我发现最新的httplib2附带了一个清理过的袜子模块,因此您真的想做更多类似的事情:
import httplib2
ht = httplib2.Http(proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, 'name_or_ip_of_the_proxy_box', proxy_port)
flow.step2_exchange(code, ht)
另外,您想使用的oauth2client> = 1.0beta8版本需要httplib2> = 0.7.4版本,在这两个软件包中都清除了对HTTP代理的支持。
添加回答
举报
0/150
提交
取消