Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions samples/Add-on/Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ function authCallback(request) {
* Logs the redict URI to register in the Google Developers Console.
*/
function logRedirectUri() {
var service = getGitHubService();
Logger.log(service.getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion samples/AdobeSign.gs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ function authCallback(request) {
* Logs the redict URI to register in the Dropbox application settings.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Basecamp.gs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/ChatWork.gs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/CloudIdentityAwareProxy.gs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ function authCallback(request) {
* Logs the redict URI to register in the Google Developers Console.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Dropbox.gs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ function authCallback(request) {
* Logs the redict URI to register in the Dropbox application settings.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Facebook.gs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/FitBit.gs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/GitHub.gs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/GooglePlus.gs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ function authCallback(request) {
* Logs the redict URI to register in the Google Developers Console.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}

2 changes: 1 addition & 1 deletion samples/Harvest.gs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Jira.gs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/LinkedIn.gs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Medium.gs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ function authCallback_(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Meetup.gs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/RingCentral.gs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Salesforce.gs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Smartsheet.gs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ function smartsheetTokenHandler(payload) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Strava.gs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ function authCallback_(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/UltraCart.gs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/VK.gs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ function authCallback(request) {
* Logs the redict URI to register in the VK Aps Page https://vk.com/apps?act=manage.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
3 changes: 1 addition & 2 deletions samples/WebApp/Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ function authCallback(request) {
* Logs the redict URI to register in the Google Developers Console.
*/
function logRedirectUri() {
var service = getGitHubService();
Logger.log(service.getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion samples/Wordpress.gs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Yahoo.gs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Yandex.gs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ function authCallback(request) {
* Logs the redict URI to register in the Yandex oAuth Page https://oauth.yandex.ru/client/new.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Zendesk.gs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
2 changes: 1 addition & 1 deletion samples/Zoom.gs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ function authCallback(request) {
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(getService().getRedirectUri());
Logger.log(OAuth2.getRedirectUri());
}
92 changes: 92 additions & 0 deletions samples/eBay.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* This sample demonstrates how to configure the library for the eBay API,
* using the authorization code flow to get a user access token.
* Instructions on how to generate OAuth credentuals is available here:
* https://developer.ebay.com/api-docs/static/oauth-qref-auth-code-grant.html
*/

var CLIENT_ID = '...'; // App ID
var CLIENT_SECRET = '...'; // Cert ID
var RU_NAME = '...'; // eBay Redirect URL name.

/**
* Authorizes and makes a request to the Ebay API.
*/
function run() {
var service = getService();
if (service.hasAccess()) {
// Sandbox environment.
var url = 'https://api.sandbox.ebay.com/sell/inventory/v1/inventory_item';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}

/**
* Reset the authorization state, so that it can be re-tested.
*/
function reset() {
getService().reset();
}

/**
* Configures the service.
*/
function getService() {
return OAuth2.createService('eBay')
// Set the endpoint URLs (sandbox environment).
.setTokenUrl('https://api.sandbox.ebay.com/identity/v1/oauth2/token')
.setAuthorizationBaseUrl('https://signin.sandbox.ebay.com/authorize')

// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)

// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')

// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())

// Set the redirect URI to the RuName (eBay Redirect URL name).
.setRedirectUri(RU_NAME)

// Set the require scopes.
.setScope('https://api.ebay.com/oauth/api_scope/sell.inventory.readonly')

// Add a Basic Authorization header to token requests.
.setTokenHeaders({
Authorization: 'Basic ' +
Utilities.base64Encode(CLIENT_ID + ':' + CLIENT_SECRET)
});
}

/**
* Handles the OAuth2 callback.
*/
function authCallback(request) {
var service = getService();
var authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}

/**
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}
8 changes: 5 additions & 3 deletions src/OAuth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ function createService(serviceName) {
/**
* Returns the redirect URI that will be used for a given script. Often this URI
* needs to be entered into a configuration screen of your OAuth provider.
* @param {string} scriptId The script ID of your script, which can be found in
* the Script Editor UI under "File > Project properties".
* @param {string} [optScriptId] The script ID of your script, which can be
* found in the Script Editor UI under "File > Project properties". Defaults
* to the script ID of the script being executed.
* @return {string} The redirect URI.
*/
function getRedirectUri(scriptId) {
function getRedirectUri(optScriptId) {
var scriptId = optScriptId || ScriptApp.getScriptId();
return 'https://script.google.com/macros/d/' + encodeURIComponent(scriptId) +
'/usercallback';
}
Expand Down
40 changes: 23 additions & 17 deletions src/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var Service_ = function(serviceName) {
this.params_ = {};
this.tokenFormat_ = TOKEN_FORMAT.JSON;
this.tokenHeaders_ = null;
this.scriptId_ = eval('Script' + 'App').getScriptId();
this.expirationMinutes_ = 60;
};

Expand Down Expand Up @@ -301,6 +300,27 @@ Service_.prototype.setGrantType = function(grantType) {
return this;
};

/**
* Sets the URI to redirect to when the OAuth flow has completed. By default the
* library will provide this value automatically, but in some rare cases you may
* need to override it.
* @param {string} redirectUri The redirect URI.
* @return {Service_} This service, for chaining.
*/
Service_.prototype.setRedirectUri = function(redirectUri) {
this.redirectUri_ = redirectUri;
return this;
};

/**
* Returns the redirect URI that will be used for this service. Often this URI
* needs to be entered into a configuration screen of your OAuth provider.
* @return {string} The redirect URI.
*/
Service_.prototype.getRedirectUri = function() {
return this.redirectUri_ || getRedirectUri();
};

/**
* Gets the authorization URL. The first step in getting an OAuth2 token is to
* have the user visit this URL and approve the authorization request. The
Expand All @@ -313,12 +333,10 @@ Service_.prototype.setGrantType = function(grantType) {
Service_.prototype.getAuthorizationUrl = function(optAdditionalParameters) {
validate_({
'Client ID': this.clientId_,
'Script ID': this.scriptId_,
'Callback function name': this.callbackFunctionName_,
'Authorization base URL': this.authorizationBaseUrl_
});

var redirectUri = getRedirectUri(this.scriptId_);
var stateTokenBuilder = eval('Script' + 'App').newStateToken()
.withMethod(this.callbackFunctionName_)
.withArgument('serviceName', this.serviceName_)
Expand All @@ -331,7 +349,7 @@ Service_.prototype.getAuthorizationUrl = function(optAdditionalParameters) {
var params = {
client_id: this.clientId_,
response_type: 'code',
redirect_uri: redirectUri,
redirect_uri: this.getRedirectUri(),
state: stateTokenBuilder.createToken()
};
params = extend_(params, this.params_);
Expand All @@ -358,15 +376,13 @@ Service_.prototype.handleCallback = function(callbackRequest) {
validate_({
'Client ID': this.clientId_,
'Client Secret': this.clientSecret_,
'Script ID': this.scriptId_,
'Token URL': this.tokenUrl_
});
var redirectUri = getRedirectUri(this.scriptId_);
var payload = {
code: code,
client_id: this.clientId_,
client_secret: this.clientSecret_,
redirect_uri: redirectUri,
redirect_uri: this.getRedirectUri(),
grant_type: 'authorization_code'
};
var token = this.fetchToken_(payload);
Expand Down Expand Up @@ -447,16 +463,6 @@ Service_.prototype.getLastError = function() {
return this.lastError_;
};

/**
* Returns the redirect URI that will be used for this service. Often this URI
* needs to be entered into a configuration screen of your OAuth provider.
* @return {string} The redirect URI.
*/
Service_.prototype.getRedirectUri = function() {
return getRedirectUri(this.scriptId_);
};


/**
* Fetches a new token from the OAuth server.
* @param {Object} payload The token request payload.
Expand Down