1

这是我用于以下操作的 JavaScript 代码:https ://api.twitter.com/1.1/users/show.json?screen_name=barackobama

自 Twitter API 1.1 起不再起作用:{"errors":[{"message":"Bad Authentication data","code":215}]}

我知道您现在需要 OAuth 访问令牌,我什至在 Twitter 中创建了一个,但我不知道如何在此脚本中添加它。

谢谢你的帮助!

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;
};
4

1 回答 1

3

根据新版本 Twitter API v1.1,所有对 Twitter 的请求都必须使用 OAuth 1.0A 进行签名。您可能想访问这里以了解它是如何工作的。我建议使用 Twitter 库来验证和签署您的请求,例如codebird-js(查看下面的示例或给定的网站)。它真的很容易使用。验证很简单,如下所示:

var cb = new Codebird;
cb.setConsumerKey('YOURKEY', 'YOURSECRET');
cb.setToken('YOURTOKEN', 'YOURTOKENSECRET');

然后拨打电话,例如推特:

cb.__call(
    'statuses_update',
    {'status': 'Whohoo, I just tweeted!'},
    function (reply) {
        // ...
    }
);

希望能帮助到你。

于 2013-06-15T21:07:07.950 回答