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会切断准确的消息。 所以在用OAuthplayground检查密钥后(为了获得更好的errormsg),我得到了这个错误:
HTTP/1.1 400 Bad Request
Content-length: 37
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
X-google-cache-control: remote-fetch
-content-encoding: gzip
Server: GSE
Via: HTTP/1.1 GWA
Pragma: no-cache
Cache-control: no-cache, no-store, max-age=0, must-revalidate
Date: Thu, 06 Jun 2013 13:54:29 GMT
X-frame-options: SAMEORIGIN
Content-type: application/json
Expires: Fri, 01 Jan 1990 00:00:00 GMT
{
"error" : "unauthorized_client"
}
我检查了client_id(已安装的应用)和client_secret与Google API控制台(https://code.google.com/apis/console/)中指定的相同。
如果我通过OAuthPlayground执行整个过程,它将起作用,但如果我尝试使用由游乐场创建的令牌,则App也会失败。
任何人都知道如何修复它?
答案 0 :(得分:1)
修正了它。我在代理后面,让step1 Auth通过但显然不是step2 auth。这么简单
h = httplib2.Http(proxy_info = httplib2.ProxyInfo PROXY DATA .....)
flow.step2_exchange(code, h)
修好了。
答案 1 :(得分:1)
有一个如何在httplib2中配置proxy_info的示例在https://code.google.com/p/httplib2/wiki/Examples
说:
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&gt; = 1.0beta8的版本,该版本需要httplib2&gt; = 0.7.4的版本,这是在两个软件包中清理对HTTP代理的支持的地方。