Skip to content

Commit 02ef999

Browse files
author
Eric Koleda
committed
Use script ID instead of project key, and bump version.
1 parent 6c2ff35 commit 02ef999

File tree

5 files changed

+52
-76
lines changed

5 files changed

+52
-76
lines changed

README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ in your project. To add it to your script, do the following in the Apps Script
1414
code editor:
1515

1616
1. Click on the menu item "Resources > Libraries..."
17-
2. In the "Find a Library" text box, enter the project key
18-
`MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48` and click the "Select" button.
17+
2. In the "Find a Library" text box, enter the script ID
18+
`1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF` and click the
19+
"Select" button.
1920
3. Choose a version in the dropdown box (usually best to pick the latest
2021
version).
2122
4. Click the "Save" button.
@@ -32,22 +33,17 @@ URL that users will be redirected to after they've authorized the token. For
3233
this library (and the Apps Script functionality in general) the URL will always
3334
be in the following format:
3435

35-
https://script.google.com/macros/d/{PROJECT KEY}/usercallback
36+
https://script.google.com/macros/d/{SCRIPT ID}/usercallback
3637

37-
Where `{PROJECT KEY}` is the key of the script that is using this library. You
38-
can find your script's project key in the Apps Script code editor by clicking on
38+
Where `{SCRIPT ID}` is the ID of the script that is using this library. You
39+
can find your script's ID in the Apps Script code editor by clicking on
3940
the menu item "File > Project properties".
4041

41-
**Warning**: Due to an
42-
[open issue](https://code.google.com/p/google-apps-script-issues/issues/detail?id=6098)
43-
in Apps Script, the project key shown in the Project properties dialog
44-
doesn't match the version the library uses. You can call the service's
45-
`getRedirectUri()` method to view the exact URL that the service will use when
46-
performing the OAuth flow. Register this version in addition to or in place of
47-
the version that uses the key shown in the dialog.
42+
Alternatively you can call the service's `getRedirectUri()` method to view the
43+
exact URL that the service will use when performing the OAuth flow:
4844

4945
/**
50-
* Logs the redict URI to register in the Google Developers Console, etc.
46+
* Logs the redict URI to register.
5147
*/
5248
function logRedirectUri() {
5349
var service = getService();

dist/OAuth2.gs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ var TOKEN_FORMAT = {
3434
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
3535
};
3636

37+
/**
38+
* The supported locations for passing the state parameter.
39+
* @type {Object.<string, string>}
40+
*/
41+
var STATE_PARAMETER_LOCATION = {
42+
AUTHORIZATION_URL: 'authorization-url',
43+
REDIRECT_URL: 'redirect-url'
44+
};
45+
3746
/**
3847
* Creates a new OAuth2 service with the name specified. It's usually best to create and
3948
* configure your service once at the start of your script, and then reference them during
@@ -48,19 +57,12 @@ function createService(serviceName) {
4857
/**
4958
* Returns the redirect URI that will be used for a given script. Often this URI
5059
* needs to be entered into a configuration screen of your OAuth provider.
51-
* @param {string} projectKey The project key of your script, which can be found in
60+
* @param {string} scriptID The script ID of your script, which can be found in
5261
* the Script Editor UI under "File > Project properties".
5362
* @return {string} The redirect URI.
5463
*/
55-
function getRedirectUri(projectKey) {
56-
return Utilities.formatString('https://script.google.com/macros/d/%s/usercallback', projectKey);
57-
}
58-
59-
if (module) {
60-
module.exports = {
61-
createService: createService,
62-
getRedirectUri: getRedirectUri
63-
};
64+
function getRedirectUri(scriptId) {
65+
return Utilities.formatString('https://script.google.com/macros/d/%s/usercallback', scriptId);
6466
}
6567

6668
// Copyright 2014 Google Inc. All Rights Reserved.
@@ -98,7 +100,7 @@ var Service_ = function(serviceName) {
98100
this.params_ = {};
99101
this.tokenFormat_ = TOKEN_FORMAT.JSON;
100102
this.tokenHeaders_ = null;
101-
this.projectKey_ = eval('Script' + 'App').getProjectKey();
103+
this.scriptId_ = eval('Script' + 'App').getScriptId();
102104
this.expirationMinutes_ = 60;
103105
};
104106

@@ -162,18 +164,6 @@ Service_.prototype.setTokenPayloadHandler = function(tokenHandler) {
162164
return this;
163165
};
164166

165-
/**
166-
* Sets the project key of the script that contains the authorization callback function (required).
167-
* The project key can be found in the Script Editor UI under "File > Project properties".
168-
* @param {string} projectKey The project key of the project containing the callback function.
169-
* @return {Service_} This service, for chaining.
170-
* @deprecated The project key is now be determined automatically.
171-
*/
172-
Service_.prototype.setProjectKey = function(projectKey) {
173-
this.projectKey_ = projectKey;
174-
return this;
175-
};
176-
177167
/**
178168
* Sets the name of the authorization callback function (required). This is the function that will be
179169
* called when the user completes the authorization flow on the service provider's website.
@@ -310,19 +300,19 @@ Service_.prototype.setExpirationMinutes = function(expirationMinutes) {
310300
/**
311301
* Gets the authorization URL. The first step in getting an OAuth2 token is to
312302
* have the user visit this URL and approve the authorization request. The
313-
* user will then be redirected back to your application using the
314-
* project key and callback function name specified, so that the flow may continue.
303+
* user will then be redirected back to your application using callback function
304+
* name specified, so that the flow may continue.
315305
* @returns {string} The authorization URL.
316306
*/
317307
Service_.prototype.getAuthorizationUrl = function() {
318308
validate_({
319309
'Client ID': this.clientId_,
320-
'Project key': this.projectKey_,
310+
'Script ID': this.scriptId_,
321311
'Callback function name': this.callbackFunctionName_,
322312
'Authorization base URL': this.authorizationBaseUrl_
323313
});
324314

325-
var redirectUri = this.getRedirectUri();
315+
var redirectUri = getRedirectUri(this.scriptId_);
326316
var state = eval('Script' + 'App').newStateToken()
327317
.withMethod(this.callbackFunctionName_)
328318
.withArgument('serviceName', this.serviceName_)
@@ -356,10 +346,10 @@ Service_.prototype.handleCallback = function(callbackRequest) {
356346
validate_({
357347
'Client ID': this.clientId_,
358348
'Client Secret': this.clientSecret_,
359-
'Project key': this.projectKey_,
349+
'Script ID': this.scriptId_,
360350
'Token URL': this.tokenUrl_
361351
});
362-
var redirectUri = this.getRedirectUri();
352+
var redirectUri = getRedirectUri(this.scriptId_);
363353
var headers = {
364354
'Accept': this.tokenFormat_
365355
};
@@ -457,7 +447,7 @@ Service_.prototype.getLastError = function() {
457447
* @return {Exception} An error, if any.
458448
*/
459449
Service_.prototype.getRedirectUri = function() {
460-
return getRedirectUri(this.projectKey_);
450+
return getRedirectUri(this.scriptId_);
461451
};
462452

463453
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "apps-script-oauth2",
3-
"version": "1.19.0",
3+
"version": "1.20.0",
44
"description": "OAuth2 for Apps Script is a library for Google Apps Script that provides the ability to create and authorize OAuth2 tokens as well as refresh them when they expire.",
55
"repository": {
66
"type": "git",

src/OAuth2.gs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* any required setup.
1818
*/
1919

20-
// Load the Underscore.js library. This library was added using the project
21-
// key "MGwgKN2Th03tJ5OdmlzB8KPxhMjh3Sh48".
20+
// Load the Underscore.js library. This library was added using the script
21+
// ID "1I21uLOwDKdyF3_W_hvh6WXiIKWJWno8yG9lB8lf1VBnZFQ6jAAhyNTRG".
2222
var _ = Underscore.load();
2323

2424
/**
@@ -30,6 +30,15 @@ var TOKEN_FORMAT = {
3030
FORM_URL_ENCODED: 'application/x-www-form-urlencoded'
3131
};
3232

33+
/**
34+
* The supported locations for passing the state parameter.
35+
* @type {Object.<string, string>}
36+
*/
37+
var STATE_PARAMETER_LOCATION = {
38+
AUTHORIZATION_URL: 'authorization-url',
39+
REDIRECT_URL: 'redirect-url'
40+
};
41+
3342
/**
3443
* Creates a new OAuth2 service with the name specified. It's usually best to create and
3544
* configure your service once at the start of your script, and then reference them during
@@ -44,17 +53,10 @@ function createService(serviceName) {
4453
/**
4554
* Returns the redirect URI that will be used for a given script. Often this URI
4655
* needs to be entered into a configuration screen of your OAuth provider.
47-
* @param {string} projectKey The project key of your script, which can be found in
56+
* @param {string} scriptID The script ID of your script, which can be found in
4857
* the Script Editor UI under "File > Project properties".
4958
* @return {string} The redirect URI.
5059
*/
51-
function getRedirectUri(projectKey) {
52-
return Utilities.formatString('https://script.google.com/macros/d/%s/usercallback', projectKey);
53-
}
54-
55-
if (module) {
56-
module.exports = {
57-
createService: createService,
58-
getRedirectUri: getRedirectUri
59-
};
60+
function getRedirectUri(scriptId) {
61+
return Utilities.formatString('https://script.google.com/macros/d/%s/usercallback', scriptId);
6062
}

src/Service.gs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var Service_ = function(serviceName) {
3333
this.params_ = {};
3434
this.tokenFormat_ = TOKEN_FORMAT.JSON;
3535
this.tokenHeaders_ = null;
36-
this.projectKey_ = eval('Script' + 'App').getProjectKey();
36+
this.scriptId_ = eval('Script' + 'App').getScriptId();
3737
this.expirationMinutes_ = 60;
3838
};
3939

@@ -97,18 +97,6 @@ Service_.prototype.setTokenPayloadHandler = function(tokenHandler) {
9797
return this;
9898
};
9999

100-
/**
101-
* Sets the project key of the script that contains the authorization callback function (required).
102-
* The project key can be found in the Script Editor UI under "File > Project properties".
103-
* @param {string} projectKey The project key of the project containing the callback function.
104-
* @return {Service_} This service, for chaining.
105-
* @deprecated The project key is now be determined automatically.
106-
*/
107-
Service_.prototype.setProjectKey = function(projectKey) {
108-
this.projectKey_ = projectKey;
109-
return this;
110-
};
111-
112100
/**
113101
* Sets the name of the authorization callback function (required). This is the function that will be
114102
* called when the user completes the authorization flow on the service provider's website.
@@ -245,19 +233,19 @@ Service_.prototype.setExpirationMinutes = function(expirationMinutes) {
245233
/**
246234
* Gets the authorization URL. The first step in getting an OAuth2 token is to
247235
* have the user visit this URL and approve the authorization request. The
248-
* user will then be redirected back to your application using the
249-
* project key and callback function name specified, so that the flow may continue.
236+
* user will then be redirected back to your application using callback function
237+
* name specified, so that the flow may continue.
250238
* @returns {string} The authorization URL.
251239
*/
252240
Service_.prototype.getAuthorizationUrl = function() {
253241
validate_({
254242
'Client ID': this.clientId_,
255-
'Project key': this.projectKey_,
243+
'Script ID': this.scriptId_,
256244
'Callback function name': this.callbackFunctionName_,
257245
'Authorization base URL': this.authorizationBaseUrl_
258246
});
259247

260-
var redirectUri = this.getRedirectUri();
248+
var redirectUri = getRedirectUri(this.scriptId_);
261249
var state = eval('Script' + 'App').newStateToken()
262250
.withMethod(this.callbackFunctionName_)
263251
.withArgument('serviceName', this.serviceName_)
@@ -291,10 +279,10 @@ Service_.prototype.handleCallback = function(callbackRequest) {
291279
validate_({
292280
'Client ID': this.clientId_,
293281
'Client Secret': this.clientSecret_,
294-
'Project key': this.projectKey_,
282+
'Script ID': this.scriptId_,
295283
'Token URL': this.tokenUrl_
296284
});
297-
var redirectUri = this.getRedirectUri();
285+
var redirectUri = getRedirectUri(this.scriptId_);
298286
var headers = {
299287
'Accept': this.tokenFormat_
300288
};
@@ -392,7 +380,7 @@ Service_.prototype.getLastError = function() {
392380
* @return {Exception} An error, if any.
393381
*/
394382
Service_.prototype.getRedirectUri = function() {
395-
return getRedirectUri(this.projectKey_);
383+
return getRedirectUri(this.scriptId_);
396384
};
397385

398386
/**

0 commit comments

Comments
 (0)