如何使用appscript向Google API授权oauth 2.0? [英] How to authorize with oauth 2.0 from appscript to Google APIs?
问题描述
有什么样的例子可以让你在AppScript中起作用吗?
我正在研究一个更干净的教程版本,但这里有一个简单的Gist,应该给你一些示例代码,工作 -
https://gist.github.com / 4079885
它仍然缺少注销,错误处理和refresh_token功能,但至少您应该可以登录并调用受oAuth 2保护的Google API(在这种情况下,它是一个配置文件API)。
您可以在此处看到它 -
https://script.google.com/macros/s/AKfycby3gHf7vlIsfOOa9C27z9kVE79DybcuJHtEnNZqT5G8LumszQG3/exec
关键是使用oAuth 2 Web Server流程。在要点中查看 getAndStoreAccessToken
函数以获取关键细节。
我希望在接下来的几周内能够发布这些内容,但希望这可以帮助您。
更新 - 在redirect_uri中添加信息
客户端密钥与将授权码返回给特定的重定向URI。
您需要在 - https ://code.google.com/apis/console/
突出显示的URI需要与发布的URI匹配(以/ exec结尾)。您可以在Publish - > Deploy as web app下的脚本编辑器中获取发布的URI。确保在进行更改时保存新版本并发布新版本(发布的URI保持不变)。
I'm playing around with AppScript and try to get an oAuth 2.0 access token.
Any sample out there how to get this working in AppScript?
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
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/AKfycby3gHf7vlIsfOOa9C27z9kVE79DybcuJHtEnNZqT5G8LumszQG3/exec
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.
UPDATE - adding in info on redirect_uri
The client secret is tied to specific redirect URIs that the authorization code is returned to.
You need to set that at - https://code.google.com/apis/console/
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).
这篇关于如何使用appscript向Google API授权oauth 2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!