【问题标题】:Exchanging authorization code with access token @ Google: 400 Bad Request使用访问令牌交换授权码 @ Google:400 错误请求
【发布时间】:2013-06-06 07:21:53
【问题描述】:

我正在尝试用访问令牌交换授权码,下面的Google Oauth Documentation. 代码只是给了我一个Error: HTTP 400: Bad Request,正文中没有任何内容。

谁能看出这段代码有什么问题?

#!/usr/bin/python
# -*- coding: utf-8 -*-
import gdata    
from gdata.youtube import YouTubeVideoEntry
from gdata.youtube.service import YouTubeService
from gdata.service import BadAuthentication, CaptchaRequired

import urllib2
import urllib   
import cjson
import urllib
import urlparse
import os, sys, random, string, argparse

from tornado import httpclient
import tornado.ioloop
import tornado.web

YT_CLIENT_ID = "redacted.apps.googleusercontent.com"
YT_CLIENT_SECRET = 'prettyrandom'


class ytOauthHandler(tornado.web.RequestHandler):
    def get(self):
        http_client = httpclient.AsyncHTTPClient()
        code = self.request.arguments['code'][0]
        post_data = {   'code'          : code, 
                        'client_id'     : YT_CLIENT_ID, 
                        'client_secret' : YT_CLIENT_SECRET, 
                        'redirect_uri'  : 'http://my.doma.in:8004/oauth_callback/', 
                        'grant_type'    : 'authorization_code'}         

        body = urllib.urlencode(post_data)                          
        url = "https://accounts.google.com/o/oauth2/token"
        http_client.fetch(url, self.handle_request, method='POST', headers={'Content-Type' : 'application/x-www-form-urlencoded'}, body=body) #Send it off!
        self.write('OK')

    def handle_request(self, response):
        print response


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        pass


application = tornado.web.Application([
    (r"/oauth_callback/", ytOauthHandler),
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8004)
    tornado.ioloop.IOLoop.instance().start()

【问题讨论】:

    标签: python oauth tornado gdata gdata-api


    【解决方案1】:

    其实,没关系。现在得到了漂亮的 200 OK

    疑难解答提示:

    1. 检查并仔细检查所有凭据(client_id、client_secret 等)
    2. 再次检查,即使您知道不需要。 ;)

    换句话说,上面是工作代码!

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 2012-03-26
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 2015-03-30
      • 2012-06-29
      • 1970-01-01
      • 2012-11-06
      相关资源
      最近更新 更多