Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade with google sheets oath2 library and avoid the redirect page not found and the copy / paste requirement #2

Open
krachtr opened this issue Jan 8, 2025 · 1 comment

Comments

@krachtr
Copy link

krachtr commented Jan 8, 2025

add in oath2 library to the apps script project as instructed here: https://github.com/googleworkspace/apps-script-oauth2

add additional callback url to the schwab app: https://script.google.com/macros/d/{SCRIPT ID}/usercallback as directed in the same oath2 instructions

click on the link loaded in sidebar after running showSidebar().

makerequest delivers account data only, (future) add in additional functions for additional calls to api using makerequest

var schwab_apikey = scriptProperties.getProperty('schwab_apikey')
var schwab_secret = scriptProperties.getProperty('schwab_secret')
var encodedCredentials = Utilities.base64Encode(schwab_apikey + ":" + schwab_secret);

function getSchwabService_() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth2.createService('schwab')

  // Set the endpoint URLs, which are the same for all Google services.
  .setAuthorizationBaseUrl('https://api.schwabapi.com/v1/oauth/authorize')
  .setTokenUrl('https://api.schwabapi.com/v1/oauth/token')

  // Set the client ID and secret
  .setClientId(schwab_apikey)
  .setClientSecret(schwab_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())

  //Schwab API requires you to set an Authorization header on access token requests
  .setTokenHeaders({
    'Authorization': 'Basic ' +
    Utilities.base64Encode(encodedCredentials)
    })

}

function authCallback(request) {
var schwabService = getSchwabService_();
var isAuthorized = schwabService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this tab');
}
}

function showSidebar() {
var schwabService = getSchwabService_();
if (!schwabService.hasAccess()) {
var authorizationUrl = schwabService.getAuthorizationUrl();
var template = HtmlService.createTemplate(
'Authorize. ' +
'Reopen the sidebar when the authorization is complete.');
template.authorizationUrl = authorizationUrl;
var page = template.evaluate();
SpreadsheetApp.getUi().showSidebar(page);
} else {
// ...
}
}

function makeRequest() {
var schwabService = getSchwabService_();
var response = UrlFetchApp.fetch('https://api.schwabapi.com/trader/v1/accounts', {
headers: {
Authorization: 'Bearer ' + schwabService.getAccessToken()
}
});
//Logger.log(response);

}

@krachtr
Copy link
Author

krachtr commented Jan 8, 2025

correction. since encodedCredentials is already encodec, change the above.

from :

.setTokenHeaders({
'Authorization': 'Basic ' +
Utilities.base64Encode(encodedCredentials)
})

to:

.setTokenHeaders({'Authorization': 'Basic ' + encodedCredentials})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant