Twitter OAuth Authentication Cannot Get Access Tokens

Asked 1 years ago, Updated 1 years ago, 113 views

I am developing a Twitter app with Google spreadsheet, but I am having trouble getting a token in the OAuth authentication section of Twitter.

Isn't there something like the localStorage feature in JavaScript?

Here's the code.

function getToken(){
  var account
  varoath_url="https://api.twitter.com/oauth/authorize?force_login=true&screen_name="+account;
  var access_url="https://api.twitter.com/oauth/access_token";
  var request_url = "https://api.twitter.com/oauth/request_token";

   varoAuthConfig=UrlFetchApp.addOAuthService("twitter");
   vara=oAuthConfig.setAccessTokenUrl(oath_url);
   var=oAuthConfig.setRequestTokenUrl(access_url);
   varoa=oAuthConfig.setAuthorizationUrl(request_url);
   var consumer=oAuthConfig.setConsumerKey(ScriptProperties.getProperty("twitterConsumerKey"));
   varsecret=oAuthConfig.setConsumerSecret(ScriptProperties.getProperty("twitterConsumerSecret"));

javascript google-spreadsheet

2022-09-30 20:33

1 Answers

  • The specified URL combination is invalid
  • oAuthConfig.set** is just a set method, so it does not return a value
  • Basically, access tokens are managed by GoogleApps, so you don't write to retrieve or save them yourself (you are automatically prompted for permission for the first time, and GoogleApps will automatically use them after that).

Based on this point, I think this is what it looks like.

function test(){
  // Twitter OAuth Settings
  varoAuthConfig=UrlFetchApp.addOAuthService("twitter");
  oAuthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
  oAuthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
  oAuthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");

  // ConsumerKey/Secret is retrieved from Script Properties
  // ScriptProperties is deprecated, so use PropertiesService
  varprops=PropertiesService.getScriptProperties();
  oAuthConfig.setConsumerKey(props.getProperty("twitterConsumerKey"));
  oAuthConfig.setConsumerSecret(props.getProperty("twitterConsumerSecret"));

  var options = {
    'oAuthServiceName': Value specified for 'twitter', // addOAuthService
    'oAuthUseToken': 'always'
  };

  var url="https://api.twitter.com/1.1/account/verify_credentials.json";
  var response=UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

When registering an app on Twitter, remember to set the CallbackURL to https://script.google.com/macros (I fell in love here).


2022-09-30 20:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.