Skip to content

Commit 972d837

Browse files
authored
Merge pull request #195 from gsuitedevs/manifest
Mention not needing the library for Google APIs
2 parents e0fe8b4 + 21c06b9 commit 972d837

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@ expire. This library uses Apps Script's
66
[StateTokenBuilder](https://developers.google.com/apps-script/reference/script/state-token-builder)
77
and `/usercallback` endpoint to handle the redirects.
88

9+
## Connecting to a Google API
10+
11+
If you are trying to connect to a Google API from Apps Script you might not need
12+
to use this library at all. Apps Script has a number of easy-to-use,
13+
[built-in services][built_in], as well as a variety of
14+
[advanced services][advanced] that wrap existing Google REST APIs.
15+
16+
Even if your API is not covered by either, you can still use Apps Script to
17+
obtain the OAuth2 token for you. Simply
18+
[edit the script's manifest][edit_manifest] to
19+
[include the additional scopes][additional_scopes] that your API requires.
20+
When the user authorizes your script they will also be asked to approve those
21+
additional scopes. Then use the method [`ScriptApp.getOAuthToken()`][scriptapp]
22+
in your code to access the OAuth2 access token the script has acquired and pass
23+
it in the `Authorization` header of a `UrlFetchApp.fetch()` call.
24+
25+
Visit the sample [`NoLibrary`](samples/NoLibrary) to see an example of how this
26+
can be done.
27+
28+
[built_in]: https://developers.google.com/apps-script/reference/calendar/
29+
[advanced]: https://developers.google.com/apps-script/advanced/admin-sdk-directory
30+
[edit_manifest]: https://developers.google.com/apps-script/concepts/manifests#editing_a_manifest
31+
[additional_scopes]: https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
32+
[scriptapp]: https://developers.google.com/apps-script/reference/script/script-app#getoauthtoken
33+
934
## Setup
1035

1136
This library is already published as an Apps Script, making it easy to include

samples/NoLibrary/Code.gs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Use the Search Console API to list the URLs of all the sites you have setup.
3+
* @see {@link https://developers.google.com/webmaster-tools/}
4+
*/
5+
function listSearchConsoleSites() {
6+
var url = 'https://www.googleapis.com/webmasters/v3/sites';
7+
var token = ScriptApp.getOAuthToken();
8+
var response = UrlFetchApp.fetch(url, {
9+
headers: {
10+
Authorization: 'Bearer ' + token
11+
}
12+
});
13+
var result = JSON.parse(response.getContentText());
14+
result.siteEntry.forEach(function(siteEntry) {
15+
Logger.log(siteEntry.siteUrl);
16+
});
17+
}

samples/NoLibrary/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Connect to a Google API without this library
2+
3+
This sample demonstrates how to connect to a Google Search Console API, which is
4+
not available natively in Apps Script. The
5+
[script's manifest file][edit_manifest]
6+
has been edited to [include the additional scope][additional_scopes] that the
7+
API requires. When a user authorizes the script they will also be asked to
8+
approve that additional scope. We can then use the method
9+
[`ScriptApp.getOAuthToken()`][scriptapp] to access the OAuth2 access token the
10+
script has acquired and pass it in the `Authorization` header of a
11+
`UrlFetchApp.fetch()` call to the API.
12+
13+
[edit_manifest]: https://developers.google.com/apps-script/concepts/manifests#editing_a_manifest
14+
[additional_scopes]: https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
15+
[scriptapp]: https://developers.google.com/apps-script/reference/script/script-app#getoauthtoken

samples/NoLibrary/appsscript.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"timeZone": "America/New_York",
3+
"dependencies": {
4+
},
5+
"exceptionLogging": "STACKDRIVER",
6+
"oauthScopes": [
7+
"https://www.googleapis.com/auth/script.external_request",
8+
"https://www.googleapis.com/auth/webmasters"
9+
]
10+
}

0 commit comments

Comments
 (0)