Skip to content

Commit c6ac217

Browse files
authored
Create salesforce.gs
An example oAuth2 with Salesforce.com. This also retrieves the instance_url which is required for all future webservice calls. The example request returns the Chatter profile for the authorized user.
1 parent 641e984 commit c6ac217

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

samples/salesforce.gs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// You must create a new connected app in your Salesforce org in order to obtain the CLIENT_ID and CLIENT_SECRET
2+
// You must retrieve the Script ID, and supply the callback URL accordingly in your connected app settings
3+
4+
var AUTHORIZE_URL = 'https://login.salesforce.com/services/oauth2/authorize';
5+
var TOKEN_URL = 'https://login.salesforce.com/services/oauth2/token';
6+
var CLIENT_ID = "xxxx";
7+
var CLIENT_SECRET = "xxxx";
8+
var REDIRECT_URL = "https://script.google.com/macros/d/{SCRIPT_ID}/usercallback";
9+
var PROPERTY_STORE = PropertiesService.getUserProperties();
10+
11+
12+
function getSalesforceService() {
13+
// Create a new service with the given name. The name will be used when
14+
// persisting the authorized token, so ensure it is unique within the
15+
// scope of the property store
16+
return OAuth2.createService('Salesforce')
17+
18+
.setAuthorizationBaseUrl(AUTHORIZE_URL)
19+
.setTokenUrl(TOKEN_URL)
20+
21+
// Set the client ID and secret, from the Salesforce Connected App
22+
.setClientId(CLIENT_ID)
23+
.setClientSecret(CLIENT_SECRET)
24+
25+
// Set the name of the callback function in the script referenced
26+
// above that should be invoked to complete the OAuth flow.
27+
.setCallbackFunction('authCallback')
28+
29+
// Set the property store where authorized tokens should be persisted.
30+
.setPropertyStore(PROPERTY_STORE)
31+
32+
// Salesforce specific params
33+
.setParam('response_type', 'code')
34+
.setParam('display', 'popup');
35+
}
36+
37+
function showSidebar() {
38+
var salesforceService = getSalesforceService();
39+
//if (!salesforceService.hasAccess()) {
40+
var authorizationUrl = salesforceService.getAuthorizationUrl();
41+
var template = HtmlService.createTemplate(
42+
'<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a>. ' +
43+
'Reopen the sidebar when the authorization is complete.');
44+
template.authorizationUrl = authorizationUrl;
45+
var page = template.evaluate();
46+
SpreadsheetApp.getUi().showSidebar(page);
47+
//DocumentApp.getUi().showSidebar(page);
48+
//} else {
49+
// ...
50+
//}
51+
}
52+
53+
function authCallback(request) {
54+
var salesforceService = getSalesforceService();
55+
var isAuthorized = salesforceService.handleCallback(request);
56+
if (isAuthorized) {
57+
return HtmlService.createHtmlOutput('Success! You can close this tab.');
58+
} else {
59+
return HtmlService.createHtmlOutput('Denied. You can close this tab');
60+
}
61+
}
62+
63+
function whoAmI() {
64+
var salesforceService = getSalesforceService();
65+
var token = salesforceService.getAccessToken();
66+
var instanceURL = salesforceService.getToken_().instance_url;
67+
var response = UrlFetchApp.fetch(instanceURL + '/services/data/v24.0/chatter/users/me', {
68+
headers: {
69+
Authorization: 'Bearer ' + token
70+
}
71+
});
72+
Logger.log(response);
73+
}
74+
75+
function onOpen(e) {
76+
SpreadsheetApp.getUi()
77+
.createMenu('Salesforce Connect')
78+
.addItem('Authorize', 'showSidebar')
79+
.addItem('WhoAmI', 'whoAmI')
80+
.addToUi();
81+
}

0 commit comments

Comments
 (0)