Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authorize with oauth 2.0 from appscript to Google APIs?

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?

like image 992
Volkmar Binder Avatar asked Nov 23 '12 17:11

Volkmar Binder


1 Answers

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/

Redirect 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).

Published URI

like image 129
Arun Nagarajan Avatar answered Oct 16 '22 23:10

Arun Nagarajan