使用Python请求库获取Google Oauth2令牌

tjb*_*tjb 5 python oauth-2.0

我花了数小时试图解决如何使用python请求库获取oauth2令牌的问题。我究竟做错了什么?

  r=requests.post("https://accounts.google.com/o/oauth2/token",
                            headers={
                                'content-type':'application/x-www-form-urlencoded'},
                            params={
                                'code':request.GET['code'],
                    'client_id':'11111111111.apps.googleusercontent.com',
                                'client_secret':'my secert',
                                'redirect_uri':'http://localhost/etc'
                                'grant_type':'authorization_code'})
Run Code Online (Sandbox Code Playgroud)

tjb*_*tjb 3

请求的数据必须在正文中发送,而不是在查询字符串中发送

在您的代码中更改params=data=

有关详细信息,请参阅此讨论: https://groups.google.com/forum/#! topic/oauth2-dev/ahP0cz2ao0o