Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Google OAuth 2.0 web应用程序查找错误项目的重定向URI_Python_Django_Oauth 2.0_Google Api_Google Authentication - Fatal编程技术网

Python Google OAuth 2.0 web应用程序查找错误项目的重定向URI

Python Google OAuth 2.0 web应用程序查找错误项目的重定向URI,python,django,oauth-2.0,google-api,google-authentication,Python,Django,Oauth 2.0,Google Api,Google Authentication,我正在尝试用Django构建一个web应用程序,它要求用户使用GoogleCalendarAPI管理他们的日历。我遵循中描述的步骤,但使用Django而不是Flask 到目前为止,我已经编写了一个名为google_calendar的视图,它获取授权URL并重定向到该URL: from django.conf import settings from django.shortcuts import redirect import google.oauth2.credentials import g

我正在尝试用Django构建一个web应用程序,它要求用户使用GoogleCalendarAPI管理他们的日历。我遵循中描述的步骤,但使用Django而不是Flask

到目前为止,我已经编写了一个名为google_calendar的视图,它获取授权URL并重定向到该URL:

from django.conf import settings
from django.shortcuts import redirect
import google.oauth2.credentials
import google_auth_oauthlib.flow


# Client configuration for an OAuth 2.0 web server application
# (cf. https://developers.google.com/identity/protocols/OAuth2WebServer)
CLIENT_CONFIG = {'web': {
    'client_id': settings.GOOGLE_CLIENT_ID,
    'project_id': settings.GOOGLE_PROJECT_ID,
    'auth_uri': 'https://accounts.google.com/o/oauth2/auth',
    'token_uri': 'https://www.googleapis.com/oauth2/v3/token',
    'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs',
    'client_secret': settings.GOOGLE_CLIENT_SECRET,
    'redirect_uris': settings.GOOGLE_REDIRECT_URIS,
    'javascript_origins': settings.GOOGLE_JAVASCRIPT_ORIGINS}}

# This scope will allow the application to manage your calendars
SCOPES = ['https://www.googleapis.com/auth/calendar']



def get_authorization_url():
    # Use the information in the client_secret.json to identify
    # the application requesting authorization.
    flow = google_auth_oauthlib.flow.Flow.from_client_config(
        client_config=CLIENT_CONFIG,
        scopes=SCOPES)

    # Indicate where the API server will redirect the user after the user completes
    # the authorization flow. The redirect URI is required.
    flow.redirect_uri = 'http://localhost:8000'

    # Generate URL for request to Google's OAuth 2.0 server.
    # Use kwargs to set optional request parameters.
    authorization_url, state = flow.authorization_url(
        # Enable offline access so that you can refresh an access token without
        # re-prompting the user for permission. Recommended for web server apps.
        access_type='offline',
        # Enable incremental authorization. Recommended as a best practice.
        include_granted_scopes='true')

    return authorization_url, state


def google_calendar(request):
    authorization_url, state = get_authorization_url()
    response = redirect(to=authorization_url)
    return response
但是,如果导航到此视图,则会出现400错误:

这里的URL不是很清晰,但如果我将其复制粘贴到浏览器中,我会被重定向到另一个项目,称为NPS Survey,而不是我想要的项目,称为Cleo。然后,我收到一条错误消息,因为我已计划删除该项目:

我很确定我输入了Cleo项目的密钥等,而不是NPS调查。为什么要在重定向uri中查找错误的项目?

flow.redirect\u uri='10〕http://localhost:8000'这个uri意味着在用户成功验证后,google IDP将重定向到。显然,你让它重定向到http://localhost:8000 在谷歌身份提供商。这是NPS调查


您应该设置为flow.redirect_uri='1!'http://yourhostip:8000'. 而且还设置了http://yourhostip:8000 在google oauth端,关于重定向url。

我实际上是在本地主机上运行这个脚本的,所以重定向URI是预期的。最后,在核动力源调查项目最终确定后,问题自行解决;但是,我仍然不知道为什么一开始它默认为错误的项目。虽然您在本地主机上运行脚本,但是您发送了flow.redirect\u uri='1!'http://localhost:8000对于GoogleIDP,您告诉GoogleIDP在认证后重定向到localhost:8000,再重定向到google的localhost:8000,这是错误的。由于google消息localhost:8000与为客户端授权的URI不匹配根据,为了进行测试,您可以指定引用本地计算机的URI,例如http://localhost:8080. 因此,我不同意在本文中“localhost”指的是您断言的Google的localhost。如果您在本地主机上运行IdP,localhost:8000就可以了。如果你使用谷歌Oauth服务,我坚持我的观点。