1

このガイドに従って、Web アプリケーションで Youtube API を使用しようとしていますhttps://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Server_Side_Web_Applications_Flowステップ 3 まで正常に完了し、** 認証コード** を取得しました.

今、私はこのURLへのPOSTリクエストを作成することに固執しましたhttps://accounts.google.com/o/oauth2/token

次のようなリクエストをしたい: (上記のリンクから取得)

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&
client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&
redirect_uri=http://localhost/oauth2callback&
grant_type=authorization_code

私はこのようなことを試しました:

headers = {'Content-Type': 'application/x-www-form-urlencoded code=4/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&client_id=xxxxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxxxxxxx&redirect_uri=http://127.0.0.1:8000/information/youtube/&grant_type=authorization_code'}

r = requests.post(url, headers)

そして、私はこのエラーが発生しています:

<Response [411]>
u'<!DOCTYPE html>\n<html lang=en>\n  <meta charset=utf-8>\n  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">\n  <title>Error 411 (Length Required)!!1</title>\n  <style>\n    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}\n  </style>\n  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>\n  <p><b>411.</b> <ins>That\u2019s an error.</ins>\n  <p>POST requests require a <code>Content-length</code> header.  <ins>That\u2019s all we know.</ins>\n'

投稿リクエストが間違っていることはわかっています。それを機能させる方法についてあなたのガイダンスが必要です。

ありがとう!

4

2 に答える 2

6

ヘッダーにデータを入れないでください。data代わりにのkwargを使用してください.post()headersurlencoded POST本体がデフォルトであるため、引数を介して指定する必要もありません。

data = {
    'code': '4/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'client_id': 'xxxxxxxxxxx.apps.googleusercontent.com',
    'client_secret': 'xxxxxxxxxxxxxxxxx',
    'redirect_uri': 'http://127.0.0.1:8000/information/youtube/'.
    'grant_type': 'authorization_code'
}
r = requests.post(url, data=data)

リクエストにrequests -oauth拡張機能を介してOAuth認証を処理させることで、このコードをさらに優れたものにすることができます: https ://github.com/maraujop/requests-oauth#readme

次に、OAuthパラメーターを手動で処理せずにリクエストを実行できます。

于 2012-06-22T18:02:23.393 に答える
3

dataのパラメータを使用する必要がありますrequests.post

ドキュメントが役立つはずです:http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

于 2012-06-22T18:02:15.213 に答える