title: How to build Twitter OAuth authentication?
body: <p>This is my JavaScript code for following operation: <a href="https://api.twitter.com/1.1/users/show.json?screen_name=barackobama" rel="nofollow noreferrer">https://api.twitter.com/1.1/users/show.json?screen_name=barackobama</a></p>
  
  <p>Does not work anymore since the Twitter API 1.1: <code>{"errors":[{"message":"Bad Authentication data","code":215}]}</code></p>
  
  <p>I know you need the OAuth access Token now, I even created one in Twitter, but I don't know how to add it in this Script.</p>
  
  <p>Thanks for any Help!</p>
  
  <pre><code>function TwitterFollowers(aUsername)
  {
    user = _twitterFetchUserData(aUsername);
    return user.followers_count;
  }
  
  function TwitterFollowings(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.friends_count;
  }
  
  function TwitterListed(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.listed_count;
  }
  
  function TwitterId(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.id;
  }
  
  function TwitterFullname(aUsername)
  {
    user = _twitterFetchUserData(aUsername);
    return user.name;
  }
  
  function TwitterCreatedDate(aUsername)
  {
    user = _twitterFetchUserData(aUsername);
    return user.created_at;
  }
  
  function TwitterVerified(aUsername)
  {
    user = _twitterFetchUserData(aUsername);
    return user.verified;
  }
  
  function TwitterTimezone(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.time_zone;
  }
  
  function TwitterLocation(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.location;
  }
  
  function TwitterHomepage(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.url;
  }
  
  function TwitterDescription(aUsername)
  {
    user = _twitterFetchUserData(aUsername);  
    return user.description;
  }
  
  
  // internal function invisible to Google SpreadSheets
  var _twitterFetchUserData = function(aUsername)
  {
    if (aUsername === undefined || aUsername === null)
      throw "No parameter specified. Write any Twitter USERNAME as parameter."
  
    if (aUsername == "")
      throw "USERNAME cannot be empty string. Write any Twitter USERNAME as parameter.";
  
    // See https://dev.twitter.com/docs/api/1/get/users/show for API documentation
    var url = "https://api.twitter.com/1.1/users/show.json?screen_name=" + encodeURIComponent(aUsername);
  
    try
    {
      var response = UrlFetchApp.fetch(url);
    }
    catch (e)
    {
      throw "Please check if Twitter Username \"" + aUsername + "\" exists. " + e;
    }
  
    if (response.getResponseCode() != 200)
      throw "Unexpected response code from Twitter.";
  
    var responseText = response.getContentText();
  
    if (responseText == null || responseText == "")
      throw "Empty response from Twitter.";
  
    var user = Utilities.jsonParse(responseText);
  
    if (user == null)
      throw "Problem with response from Twitter. Invalid JSON.";
  
    return user;
  };
  </code></pre>