gpt4 book ai didi

angular - Google SignIn 在 Web App 中获取访问 token

转载 作者:太空狗 更新时间:2023-10-29 19:31:30 24 4
gpt4 key购买 nike

在我的网络应用程序(FE:Angular,BE:Flask)中,我要求用户使用 Google 登录并将刷新 token 保存在我的数据库中。

通过谷歌文档后,我了解到刷新 token 只能在服务器端应用程序中获取。但是,文档中指定的代码似乎不起作用。引用:https://developers.google.com/identity/sign-in/web/server-side-flow

我在第 7 步中遇到错误,https://developers.google.com/identity/sign-in/web/server-side-flow#step_7_exchange_the_authorization_code_for_an_access_token

from apiclient import discovery
import httplib2
from oauth2client import client

# (Receive auth_code by HTTPS POST)


# If this request does not have `X-Requested-With` header, this could be a CSRF
if not request.headers.get('X-Requested-With'):
abort(403)

# Set path to the Web application client_secret_*.json file you downloaded from the
# Google API Console: https://console.developers.google.com/apis/credentials
CLIENT_SECRET_FILE = '/path/to/client_secret.json'

# Exchange auth code for access token, refresh token, and ID token
credentials = client.credentials_from_clientsecrets_and_code(
CLIENT_SECRET_FILE,
['https://www.googleapis.com/auth/drive.appdata', 'profile', 'email'],
auth_code)

# Call Google API
http_auth = credentials.authorize(httplib2.Http())
drive_service = discovery.build('drive', 'v3', http=http_auth)
appfolder = drive_service.files().get(fileId='appfolder').execute()

# Get profile info from ID token
userid = credentials.id_token['sub']
email = credentials.id_token['email']

最佳答案

我找到了一个解决方案,对以下响应稍作修改,https://stackoverflow.com/a/50616780/11997783

更改登录功能

this.auth2.signIn().then(user => {})

signIn(): void {
this.auth2.grantOfflineAccess().then((resp) => {});

使用 grantOfflineAccess() 我们将能够获得授权码。

POST the Authorization code to https://oauth2.googleapis.com/token with parameters 
code: <authorization-code> ,
client_id: <project-client-id>,
client_secret: <project-client-secret>,
redirect_uri: <should be uri of the FE>,
grant_type: authorization_code

附加引用:https://developers.google.com/identity/sign-in/web/reference#gapiauth2clientconfig

关于angular - Google SignIn 在 Web App 中获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com