【问题标题】:Why oauth2 url is not opened on my client side?为什么我的客户端没有打开 oauth2 url?
【发布时间】:2021-10-30 21:36:55
【问题描述】:

我的 google drive oauth2 有问题。

我有这个代码:

def getService(creds):

    service = build('drive', 'v3', credentials=creds)
    return service


def getCredentials():

    # If modifying these scopes, delete the file token.json.
    SCOPES = ['https://www.googleapis.com/auth/drive']
    """Shows basic usage of the Drive v3 API.
        Prints the names and ids of the first 10 files the user has access to.
        """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:

            creds.refresh(Request())
        else:

            flow = InstalledAppFlow.from_client_secrets_file(
                'code_clientXXX.json',
                SCOPES)

            creds = flow.run_local_server(port=0)
            print(creds)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    return creds

当我在 localhost 中运行时,我工作得很好,这个 url 会在我的浏览器上自动打开: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=XXXX&[..............]

但是,当我使用 docker 在远程服务器上部署我的前端(角度应用程序)和后端(django 应用程序)时。有问题。

1 - 如果我的 token.json 已经生成:效果很好。

2 - 如果我的 token.json 尚未生成:客户端不询问任何内容。

如果我检查 django 日志:

sudo docker logs c289011c7be6
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=XXX-XXX.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A46523%2F&sc
ope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=XXX&access_type=offline

1- 为什么我的客户端没有打开这个网址

2- 为什么重定向是 localhost 而不是我的 ip 地址?

非常感谢

【问题讨论】:

    标签: oauth-2.0 google-drive-api


    【解决方案1】:
     flow = InstalledAppFlow.from_client_secrets_file(
                'code_clientXXX.json',
                SCOPES)
    

    Oauth.md

    google_auth_oauthlib.flow.InstalledAppFlow 类用于安装的应用程序。此流程对于本地开发或安装在桌面操作系统上的应用程序很有用。请参阅 OAuth 2.0 了解已安装的应用程序。

    您的代码设计为作为已安装的应用程序运行,这意味着同意屏幕将在运行代码的机器上打开。如果您尝试深入了解 docker 将尝试在 docker 容器中打开一个浏览器窗口,但该窗口无法正常工作,没有人可以看到它。

    【讨论】:

    • 您好!非常感谢您的回复!你知道我可以使用哪个功能来实现我想要的吗?我必须更改我的所有代码还是我必须找到解决方案的只是这个功能?
    • 不,我还没有找到任何可以让您在客户端浏览器googleapis.github.io/google-api-python-client/docs/oauth.html 中打开它的东西,据我所知,该库仅打算作为已安装的应用程序运行。
    • 感谢您的帮助!很奇怪..应该有一种方法可以从客户端创建令牌..我想我不是唯一一个想通过 Web 应用程序生成令牌的人。
    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2013-11-02
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    相关资源
    最近更新 更多