0

Google Oauth ドキュメントに従って、認証コードをアクセス トークンと交換しようとしています。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()
4

1 に答える 1

1

実際、気にしないでください。美しい200 OKを手に入れました。

トラブルシューティングのヒント:

  1. すべての資格情報 (client_id、client_secret など) を確認して再確認します。
  2. 必要ないとわかっていても、もう一度確認してください。;)

言い換えれば、上記は動作するコードです!

于 2013-06-05T23:21:53.720 に答える