Python 谷歌oauth如何获取;代码“;从步骤2 url

Python 谷歌oauth如何获取;代码“;从步骤2 url,python,django,api,oauth,Python,Django,Api,Oauth,我正在尝试使用GoogleOAuth进行身份验证,但在遵循教程时遇到了一些问题 以下是我当前的设置: FLOW = OAuth2WebServerFlow( client_id='67490467925.apps.googleusercontent.com', client_secret='K1tkrPK97B2W16ZGY', scope='https://www.googleapis.com/auth/calendar',

我正在尝试使用GoogleOAuth进行身份验证,但在遵循教程时遇到了一些问题

以下是我当前的设置:

    FLOW = OAuth2WebServerFlow(
        client_id='67490467925.apps.googleusercontent.com',
        client_secret='K1tkrPK97B2W16ZGY',
        scope='https://www.googleapis.com/auth/calendar',
        user_agent='Real_Hub/1.0',
        redirect_uri='http://127.0.0.1:8000/',)


    storage = Storage('calendar.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid == True:
        auth_uri = FLOW.step1_get_authorize_url()
        return auth_uri
    else:
        http = httplib2.Http()
        http = credentials.authorize(http)

        service = build(serviceName='calendar', version='v3', http=http,
                        developerKey='AIzaSyCBGjIQ2uNbThW_2oMO9P-Ufb8kc')

        return service
        #End OAUTH...

我不确定应该将
credentials=flow.step2\u交换(代码)
storage.put(credentials)
放在哪里,以及如何获取“code”变量?在API中,它表示来自重定向url。但是我不知道如何做到这一点。

您需要定义一个方法来处理来自OAuth提供程序的回调,然后将该回调方法映射到应用程序上的url,如

  http://yourserver/auth_callback
然后在创建流类时,将重定向uri设置为身份验证回调url

  FLOW = OAuth2WebServerFlow(
    client_id='67490467925.apps.googleusercontent.com',
    ...
    redirect_uri='http://yourserver/auth_callback')
获取
auth_uri
后,需要将用户重定向到该uri,以便他们能够进行身份验证/授权

  self.redirect(auth_uri, ...)
在身份验证/授权之后,OAuth提供程序将“回调”到您先前指定的重定向uri。在回调处理程序方法中,您现在将解析
code
,如果它不存在,则检查
error
参数

  code = self.request.get("code")
  credentials = FLOW.step2_exchange(code)

注意:我还没有测试过这个,我也没有用python,所以语法可能会关闭,但希望您能得到一个总体的想法。

我刚刚继续读了API,然后计算出来,但是这个答案是正确的(:谢谢。这个答案中的自我是什么?