gpt4 book ai didi

javascript - 如何在Angular 5应用程序中从Gmail API获取authorization_code

转载 作者:行者123 更新时间:2023-12-03 01:19:09 32 4
gpt4 key购买 nike

我正在使用 Angular 应用,我想在此 Angular 应用中添加 Gmail。

我已经遵循了本教程 https://medium.com/@mcflyDev/angular-2-or-4-import-google-contacts-d0ffb13d8626

一切正常,我收到了 access_token 的成功响应,但没有收到 authorization_code

如何获得authorization_code

这是我的配置:

     this.auth2 = gapi.auth2.init({
client_id: 'bla-bla-bla-2.apps.googleusercontent.com',
cookiepolicy: 'single_host_origin',
scope: 'https://www.googleapis.com/auth/gmail.readonly',
access_type: 'offline',
response_type: 'code',
auth_uri: 'https://accounts.google.com/o/oauth2/v2/auth',
prompt: 'select_account',
include_granted_scopes: true,
grant_type: 'authorization_code',
});

此外,我也没有获得 refresh_token,正如您所看到的,我已经设置了 access_type: 'offline'

enter image description here

我收到的回复如上图所示。

谢谢。

最佳答案

我找到了解决方案。早些时候,我进行了 POST Ajax 调用,但这不是必需的。

我们只需要分两步进行一个小设置:

第 1 步:准备 URL:

URL 应如下所示:

 https://accounts.google.com/o/oauth2/auth?redirect_uri=:your_redirect_url&response_type=code&client_id=:your_client_id&scope=https://www.googleapis.com/auth/gmail.send&approval_prompt=force&access_type=offline

您可以将此 URL 粘贴到浏览器中,然后按 Enter 键检查其是否正常工作。

步骤 2. 在 HTML 中创建 anchor 标记:

<a href="giveAboveURLHere">Connect With Gmail</a>

恭喜!!!你已经完成了。

每当用户点击此链接时,Google 都会请求所提供范围的许可,如果最终用户授予访问权限,则 Google 将使用 重定向到给定的 redirect_url查询参数中的authorization_code

然后在服务器端,您需要使用此 authorization_code 进行另一个 API 调用,然后 Google 将提供 access_token刷新 token

谢谢,希望对你有帮助。

关于javascript - 如何在Angular 5应用程序中从Gmail API获取authorization_code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51841868/

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