如何使用 oauth 2.0 从 appscript 到 Google API 进行授权? [英] How to authorize with oauth 2.0 from appscript to Google APIs?

查看:23
本文介绍了如何使用 oauth 2.0 从 appscript 到 Google API 进行授权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AppScript 并尝试获取 oAuth 2.0 访问令牌.

I'm playing around with AppScript and try to get an oAuth 2.0 access token.

有没有如何在 AppScript 中实现此功能的示例?

Any sample out there how to get this working in AppScript?

推荐答案

我正在开发一个更清晰的教程版本,但这里有一个简单的 Gist,应该会给你一些关于事情如何工作的示例代码 -

I am working on a cleaner tutorialized version of this, but here is a simple Gist that should give you some sample code on how things would work -

https://gist.github.com/4079885

它仍然缺少注销、错误处理和 refresh_token 功能,但至少您应该能够登录并调用受 oAuth 2 保护的 Google API(在本例中为配置文件 API).

It still lacks logout, error handling and the refresh_token capability, but at least you should be able to log in and call a oAuth 2 protected Google API (in this case its a profile API).

你可以在这里看到它的作用 -

You can see it in action here -

https://script.google.com/macros/s/AKfyc7vlIsfOOa9C27z9kVE79DybcuJHtEnNZqT5G8LumszQG3/exec" rel="noreferrer">https://script.google.com/macros/s/AKfyc7vlIsfOOa9C27z9kVE79DybcuJHtEnNZqT5G8LumszQG3/exec/a>

https://script.google.com/macros/s/AKfycby3gHf7vlIsfOOa9C27z9kVE79DybcuJHtEnNZqT5G8LumszQG3/exec

关键是使用 oAuth 2 Web Server 流程​​.查看要点中的 getAndStoreAccessToken 函数以获取关键详细信息.

The key is to use oAuth 2 Web Server flow. Take a look at getAndStoreAccessToken function in the gist to get the key details.

我希望在接下来的几周内发布此内容,但同时希望这会有所帮助.

I hope to have this published in the next few weeks but hopefully this will help in the mean time.

更新 - 添加有关 redirect_uri 的信息

客户端密钥与授权代码返回的特定重定向 URI 相关联.

The client secret is tied to specific redirect URIs that the authorization code is returned to.

您需要将其设置在 - https://code.google.com/apis/console/

You need to set that at - https://code.google.com/apis/console/

突出显示的 URI 需要匹配发布的 URI(以/exec 结尾).您可以从脚本编辑器的发布 -> 部署为 Web 应用程序下获取发布的 URI.确保在进行更改时保存新版本并发布新版本(发布的 URI 保持不变).

The highlighted URI needs to match the published URI (ends in /exec). You get the published URI from the script editor under Publish -> Deploy as web app. Make sure you are saving new versions and publishing the new versions when you make changes (the published URI stays the same).

这篇关于如何使用 oauth 2.0 从 appscript 到 Google API 进行授权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆