Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 87fca80

Browse files
author
Eric Koleda
committed
Use script ID instead of project key. Fixes #26.
1 parent b0dd75a commit 87fca80

File tree

4 files changed

+62
-48
lines changed

4 files changed

+62
-48
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ in your project. To add it to your script, do the following in the Apps Script
1717
code editor:
1818

1919
1. Click on the menu item "Resources > Libraries..."
20-
2. In the "Find a Library" text box, enter the project key
21-
"Mb2Vpd5nfD3Pz-_a-39Q4VfxhMjh3Sh48" and click the "Select" button.
20+
2. In the "Find a Library" text box, enter the script ID
21+
`1CXDCY5sqT9ph64fFwSzVtXnbjpSfWdRymafDrtIZ7Z_hwysTY7IIhi7s` and click the
22+
"Select" button.
2223
3. Choose a version in the dropdown box (usually best to pick the latest
2324
version).
2425
4. Click the "Save" button.
2526

27+
Alternatively, you can copy and paste the files in the [`/dist`](dist) directory
28+
directly into your script project.
29+
2630

2731
## Callback URL
2832

@@ -33,11 +37,23 @@ URL that users will be redirected to after they've authorized the token. For
3337
this library (and the Apps Script functionality in general) the URL will always
3438
be in the following format:
3539

36-
https://script.google.com/macros/d/{PROJECT KEY}/usercallback
40+
https://script.google.com/macros/d/{SCRIPT ID}/usercallback
41+
42+
Where `{SCRIPT ID}` is the ID of the script that is using this library. You
43+
can find your script's ID in the Apps Script code editor by clicking on the menu
44+
item "File > Project properties".
45+
46+
Alternatively you can call the service's `getCallbackUrl()` method to view the
47+
exact URL that the service will use when performing the OAuth flow:
48+
49+
/**
50+
* Logs the callback URL to register.
51+
*/
52+
function logCallbackUrl() {
53+
var service = getService();
54+
Logger.log(service.getCallbackUrl());
55+
}
3756

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

4258

4359
## Usage

dist/OAuth1.gs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ MemoryProperties.prototype.setProperty = function(key, value) {
8484
* required setup.
8585
*/
8686

87-
// Load the Underscore.js library. This library was added using the project
88-
// key "MGwgKN2Th03tJ5OdmlzB8KPxhMjh3Sh48".
87+
// Load the Underscore.js library. This library was added using the script ID
88+
// "1I21uLOwDKdyF3_W_hvh6WXiIKWJWno8yG9lB8lf1VBnZFQ6jAAhyNTRG".
8989

9090

9191
/**
@@ -102,13 +102,13 @@ function createService(serviceName) {
102102
/**
103103
* Returns the callback URL that will be used for a given script. Often this URL
104104
* needs to be entered into a configuration screen of your OAuth provider.
105-
* @param {string} projectKey The project key of your script, which can be found
106-
* in the Script Editor UI under "File > Project properties".
105+
* @param {string} scriptId The ID of your script, which can be found in the
106+
* Script Editor UI under "File > Project properties".
107107
* @return {string} The callback URL.
108108
*/
109-
function getCallbackUrl(projectKey) {
109+
function getCallbackUrl(scriptId) {
110110
return Utilities.formatString(
111-
'https://script.google.com/macros/d/%s/usercallback', projectKey);
111+
'https://script.google.com/macros/d/%s/usercallback', scriptId);
112112
}
113113

114114
if (module) {
@@ -153,7 +153,7 @@ var Service_ = function(serviceName) {
153153
this.paramLocation_ = 'auth-header';
154154
this.method_ = 'get';
155155
this.oauthVersion_ = '1.0a';
156-
this.projectKey_ = eval('Script' + 'App').getProjectKey();
156+
this.scriptId_ = eval('Script' + 'App').getScriptId();
157157
this.signatureMethod_ = 'HMAC-SHA1';
158158
this.propertyStore_ = new MemoryProperties();
159159
};
@@ -244,16 +244,16 @@ Service_.prototype.setOAuthVersion = function(oauthVersion) {
244244
};
245245

246246
/**
247-
* Sets the project key of the script that contains the authorization callback
248-
* function (required). The project key can be found in the Script Editor UI
247+
* Sets the ID of the script that contains the authorization callback
248+
* function (required). The script ID can be found in the Script Editor UI
249249
* under "File > Project properties".
250-
* @param {string} projectKey The project key of the project containing the
251-
* callback function.
250+
* @param {string} scriptId The ID of the script containing the callback
251+
* function.
252252
* @return {Service_} This service, for chaining.
253-
* @deprecated The project key is now be determined automatically.
253+
* @deprecated The script ID is now be determined automatically.
254254
*/
255-
Service_.prototype.setProjectKey = function(projectKey) {
256-
this.projectKey_ = projectKey;
255+
Service_.prototype.setScriptId = function(scriptId) {
256+
this.scriptId_ = scriptId;
257257
return this;
258258
};
259259

@@ -343,7 +343,7 @@ Service_.prototype.setAccessToken = function(token, secret) {
343343
* Starts the authorization process. A new token will be generated and the
344344
* authorization URL for that token will be returned. Have the user visit this
345345
* URL and approve the authorization request. The user will then be redirected
346-
* back to your application using the project key and callback function name
346+
* back to your application using the script ID and callback function name
347347
* specified, so that the flow may continue.
348348
* @returns {string} The authorization URL for a new token.
349349
*/
@@ -359,7 +359,7 @@ Service_.prototype.authorize = function() {
359359
oauth_token: token.public
360360
};
361361
if (this.oauthVersion_ == '1.0') {
362-
oauthParams.oauth_callback = this.getCallbackUrl_();
362+
oauthParams.oauth_callback = this.getCallbackUrl();
363363
}
364364
return buildUrl_(this.authorizationUrl_, oauthParams);
365365
};
@@ -446,7 +446,7 @@ Service_.prototype.getRequestToken_ = function() {
446446
};
447447
var oauthParams = {};
448448
if (this.oauthVersion_ == '1.0a') {
449-
oauthParams.oauth_callback = this.getCallbackUrl_();
449+
oauthParams.oauth_callback = this.getCallbackUrl();
450450
}
451451

452452
var response = this.fetchInternal_(url, params, null, oauthParams);
@@ -637,20 +637,19 @@ Service_.prototype.getPropertyKey_ = function() {
637637
/**
638638
* Gets a callback URL to use for the OAuth flow.
639639
* @return {string} A callback URL.
640-
* @private
641640
*/
642-
Service_.prototype.getCallbackUrl_ = function() {
641+
Service_.prototype.getCallbackUrl = function() {
643642
validate_({
644643
'Callback Function Name': this.callbackFunctionName_,
645644
'Service Name': this.serviceName_,
646-
'Project Key': this.projectKey_
645+
'Script ID': this.scriptId_
647646
});
648647
var stateToken = eval('Script' + 'App').newStateToken()
649648
.withMethod(this.callbackFunctionName_)
650649
.withArgument('serviceName', this.serviceName_)
651650
.withTimeout(3600)
652651
.createToken();
653-
return buildUrl_(getCallbackUrl(this.projectKey_), {
652+
return buildUrl_(getCallbackUrl(this.scriptId_), {
654653
state: stateToken
655654
});
656655
};

src/OAuth1.gs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* 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 ID
21+
// "1I21uLOwDKdyF3_W_hvh6WXiIKWJWno8yG9lB8lf1VBnZFQ6jAAhyNTRG".
2222
var _ = Underscore.load();
2323

2424
/**
@@ -35,13 +35,13 @@ function createService(serviceName) {
3535
/**
3636
* Returns the callback URL that will be used for a given script. Often this URL
3737
* needs to be entered into a configuration screen of your OAuth provider.
38-
* @param {string} projectKey The project key of your script, which can be found
39-
* in the Script Editor UI under "File > Project properties".
38+
* @param {string} scriptId The ID of your script, which can be found in the
39+
* Script Editor UI under "File > Project properties".
4040
* @return {string} The callback URL.
4141
*/
42-
function getCallbackUrl(projectKey) {
42+
function getCallbackUrl(scriptId) {
4343
return Utilities.formatString(
44-
'https://script.google.com/macros/d/%s/usercallback', projectKey);
44+
'https://script.google.com/macros/d/%s/usercallback', scriptId);
4545
}
4646

4747
if (module) {

src/Service.gs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var Service_ = function(serviceName) {
3333
this.paramLocation_ = 'auth-header';
3434
this.method_ = 'get';
3535
this.oauthVersion_ = '1.0a';
36-
this.projectKey_ = eval('Script' + 'App').getProjectKey();
36+
this.scriptId_ = eval('Script' + 'App').getScriptId();
3737
this.signatureMethod_ = 'HMAC-SHA1';
3838
this.propertyStore_ = new MemoryProperties();
3939
};
@@ -124,16 +124,16 @@ Service_.prototype.setOAuthVersion = function(oauthVersion) {
124124
};
125125

126126
/**
127-
* Sets the project key of the script that contains the authorization callback
128-
* function (required). The project key can be found in the Script Editor UI
127+
* Sets the ID of the script that contains the authorization callback
128+
* function (required). The script ID can be found in the Script Editor UI
129129
* under "File > Project properties".
130-
* @param {string} projectKey The project key of the project containing the
131-
* callback function.
130+
* @param {string} scriptId The ID of the script containing the callback
131+
* function.
132132
* @return {Service_} This service, for chaining.
133-
* @deprecated The project key is now be determined automatically.
133+
* @deprecated The script ID is now be determined automatically.
134134
*/
135-
Service_.prototype.setProjectKey = function(projectKey) {
136-
this.projectKey_ = projectKey;
135+
Service_.prototype.setScriptId = function(scriptId) {
136+
this.scriptId_ = scriptId;
137137
return this;
138138
};
139139

@@ -223,7 +223,7 @@ Service_.prototype.setAccessToken = function(token, secret) {
223223
* Starts the authorization process. A new token will be generated and the
224224
* authorization URL for that token will be returned. Have the user visit this
225225
* URL and approve the authorization request. The user will then be redirected
226-
* back to your application using the project key and callback function name
226+
* back to your application using the script ID and callback function name
227227
* specified, so that the flow may continue.
228228
* @returns {string} The authorization URL for a new token.
229229
*/
@@ -239,7 +239,7 @@ Service_.prototype.authorize = function() {
239239
oauth_token: token.public
240240
};
241241
if (this.oauthVersion_ == '1.0') {
242-
oauthParams.oauth_callback = this.getCallbackUrl_();
242+
oauthParams.oauth_callback = this.getCallbackUrl();
243243
}
244244
return buildUrl_(this.authorizationUrl_, oauthParams);
245245
};
@@ -326,7 +326,7 @@ Service_.prototype.getRequestToken_ = function() {
326326
};
327327
var oauthParams = {};
328328
if (this.oauthVersion_ == '1.0a') {
329-
oauthParams.oauth_callback = this.getCallbackUrl_();
329+
oauthParams.oauth_callback = this.getCallbackUrl();
330330
}
331331

332332
var response = this.fetchInternal_(url, params, null, oauthParams);
@@ -517,20 +517,19 @@ Service_.prototype.getPropertyKey_ = function() {
517517
/**
518518
* Gets a callback URL to use for the OAuth flow.
519519
* @return {string} A callback URL.
520-
* @private
521520
*/
522-
Service_.prototype.getCallbackUrl_ = function() {
521+
Service_.prototype.getCallbackUrl = function() {
523522
validate_({
524523
'Callback Function Name': this.callbackFunctionName_,
525524
'Service Name': this.serviceName_,
526-
'Project Key': this.projectKey_
525+
'Script ID': this.scriptId_
527526
});
528527
var stateToken = eval('Script' + 'App').newStateToken()
529528
.withMethod(this.callbackFunctionName_)
530529
.withArgument('serviceName', this.serviceName_)
531530
.withTimeout(3600)
532531
.createToken();
533-
return buildUrl_(getCallbackUrl(this.projectKey_), {
532+
return buildUrl_(getCallbackUrl(this.scriptId_), {
534533
state: stateToken
535534
});
536535
};

0 commit comments

Comments
 (0)