Search code examples
google-apps-scripttwitter-oauth

OAuth1 - Google Script cannot create Twitter Service


I got trouble with Google Script connect to Twitter via API, using OAuth1 authentication. The following images are my settings in Twitter Apps and Google Script Libraries.

Sorry I don't have enough reputation to post image

Image 1:

lh3.googleusercontent.com/7QtiFZPG8z-wAAnJQslsMjncGB0NAu0qQdpZnwiqezJ2HdHOyey90CoCfWkqIm8igzuwOhjypw0Mm7RJaAy_0vMhe0wYNOcdHzeF2zAAwbdk4uvBhk3-vLEM-i2ZQ8uuzelt63PRCD2kOvgEG_x__HxmVN_JAXh3u5ixCbDjovq3HggB_Fz04_lfX28fCdRW6VgQQLg4XK_AEN8INXtXz9OUA8Og3pdfw_GWBXw_zSdtX9KvQ1FaQ6FB7m7ACE5PPfpYRuRpRx3rbu_pw1LIbRdj32RnVcC7__TDWtH8LMDg1Rp1RfL8cImiEbzWSI1c-vGW4jNl27Y1BiSthXmkFeBBDP36q732czWy3nmNpope0I4IeAamNqnWvGxSIWyhHuL5OGEXCbUw7j4CuCQjHG3SKVTcozPPyZ1N8e5er1AUZjTG1vU1QUatV02vyz7s4a-R9MSBvYGfvvRoRiLzBBkqL_SMRPHdAOAZ5SS9x9YHpbCkWU2TXy7mVm9DefwUJwS1TNKGWWKwT67oqax6QVsu0BTBLbXhKIGIK14XkVk=w747-h486-no

Image 2:

lh3.googleusercontent.com/RHxefIpYhZLJ3Cm_UyEVFJrlqL0DGkQnhPs1SAyBeVSBEMxqbtJGWxcByUwa1-pSo9DBbwhNMb9Q474Da3WIs5NSYJbq-MEbuIwTfsYU18MDzH2WUXAwkzOa1LkoqnivuCdMn0Ii-gYlKLb94kTsd6Z_qRmlpSJQSfKAREB6SgN-v41YWEGtxdJRKK7qSeDPG2NQurhBmM58wCXccptoamNyt9m6xIyEJ-hVEh_LNJk7Tj6ZeDbSPtW6_kGHASLU3UvUsPNOnY1FdU61IePYAnXrj0AYq0erTiUPWPICkszb8rlvDaKTz7QxxRRqIPhIABclwO6mI8Br500P60ceGKI-bW4ufv56blRE8Ngr2teNaGqEAHFhVTjiltW8I2ngVc7O53zIEu3sce3IDhJast1NeFGjYN0T0OBvOj_hpU9UAhotmd-NCU-YShM6eB62cIaMopdo-VFB6cR1wyH65qlxIA9SeJe7XmPxli1Fd3ZPpLqEHmOYWMnnkuLYursyiKamxZNHxs4ywheDmAjxf6DHB6bgHwF5Sdzpyxcc5F8=w761-h461-no

And here is my code to get Twitter Service

enter code here
//Create Twitter Service
function getTwitterService_() {  
// 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 OAuth1.createService('twitter')
  // Set the endpoint URLs.
  .setAccessTokenUrl('https:// api.twitter.com/oauth/access_token')
  .setRequestTokenUrl('https:// api.twitter.com/oauth/request_token')
  .setAuthorizationUrl('https:// api.twitter.com/oauth/authorize')

  // Set the consumer key and secret.
  // Of course these values I will collect from Twitter App Settings
  .setConsumerKey("xxxxxzJzPXqH")
  .setConsumerSecret("xxxxxx")

  // 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());
}

I always got the error from OAuth1 service Service not authorized. (line 271, file "")

I really really appreciate if someone can get me out of this trouble.


Solution

  • When you create an app in Twitter, you must authorize the account you will be reading / writing via API to.

    Check the OAuth1 documentation, points 2 and 3 regards to this:

    https://github.com/googlesamples/apps-script-oauth1

    1. Create a request token and direct the user to the authorization URL --> Debug function "showSidebar" in Google App Scripts. Debug before the "template" code line, then you can copy / paste the link in the browser and authorize the app (link is stored in authorizationUrl variable).

    My Google App Script is not linked to any spreadsheet or document, that's why I have to debug script to get the url.

    After you granted or denied permission, Twitter redirects to your callback :

    1. Handle the callback

    It will show you in a new tab that your app is granted / denied permission.

    Then you can make request to twitter via API as needed.

    If you have revoked authorization in your Twitter account, you must execute

    Resetting the access token (clearService function)

    ...which clears stored auth data, and perform 2 again.

    I'd love to show you some images of this proccess, but like you, my account does not have enough reputation (and it's almost impossible to earn some when you have few or none).