Google Scripts + Twitter API - POST阻止/创建

时间:2017-12-28 10:41:19

标签: twitter google-sheets-api

我收集了一些功能,并将它们捆绑到我用于Google表格的单个Google Scripts文件中。功能包括:(1)getTwitterUserFollowers(id),(2)getTwitterUserFollowing(id),(3)getTwitterUserName(id)和(4)postBlockTwitterUser(id)。

虽然前3个功能完美无缺,但我无法使用第4个功能(postBlockTwitterUser(id)。继续获取:

"Request failed for https://api.twitter.com/1.1/blocks/create.json?screen_name=EladRatson returned code 403. Truncated server response: {"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."

我做错了什么?

var CONSUMER_KEY = 'M2VyQoC2571PyNAdHmOnGYxMj';
var CONSUMER_SECRET = '8yNJGrp2W0fDRQvXgbe8zOgQWC6lQOjrxhI7M6iogLbHyanor0';



function getTwitterUserFollowers(id) {

// Encode consumer key and secret
  var tokenUrl = "https://api.twitter.com/oauth2/token";
  var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

//  Obtain a bearer token with HTTP POST request
  var tokenOptions = {
    headers : {
      Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
    },
  method: "post",
  payload: "grant_type=client_credentials"
};

  var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
  var parsedToken = JSON.parse(responseToken);
  var token = parsedToken.access_token;

// Authenticate Twitter API requests with the bearer token
  var apiUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='+id;
  var apiOptions = {
    headers : {
      Authorization: 'Bearer ' + token
    },
  "method" : "get"
  };

  var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);
  var result = "";

  if (responseApi.getResponseCode() == 200) {

    // Parse the JSON encoded Twitter API response
    var tweets = JSON.parse(responseApi.getContentText());
    return tweets.followers_count
  }

  Logger.log(result);

}



function getTwitterUserFollowing(id) {

 // Encode consumer key and secret
 var tokenUrl = "https://api.twitter.com/oauth2/token";
 var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

 //  Obtain a bearer token with HTTP POST request
 var tokenOptions = {
   headers : {
     Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
   },
   method: "post",
   payload: "grant_type=client_credentials"
 };

 var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
 var parsedToken = JSON.parse(responseToken);
 var token = parsedToken.access_token;

 // Authenticate Twitter API requests with the bearer token
 var apiUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='+id;
 var apiOptions = {
   headers : {
     Authorization: 'Bearer ' + token
   },
   "method" : "get"
 };

 var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

 var result = "";

 if (responseApi.getResponseCode() == 200) {

   // Parse the JSON encoded Twitter API response
   var tweets = JSON.parse(responseApi.getContentText());
   return tweets.friends_count

 }

 Logger.log(result);

}



function getTwitterUserName(id) {

 // Encode consumer key and secret
 var tokenUrl = "https://api.twitter.com/oauth2/token";
 var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

 //  Obtain a bearer token with HTTP POST request
 var tokenOptions = {
   headers : {
     Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
   },
   method: "post",
   payload: "grant_type=client_credentials"
 };

 var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
 var parsedToken = JSON.parse(responseToken);
 var token = parsedToken.access_token;

 // Authenticate Twitter API requests with the bearer token
 var apiUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='+id;
 var apiOptions = {
   headers : {
     Authorization: 'Bearer ' + token
   },
   "method" : "get"
 };

 var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

 var result = "";

 if (responseApi.getResponseCode() == 200) {

   // Parse the JSON encoded Twitter API response
   var tweets = JSON.parse(responseApi.getContentText());
   return tweets.name

 }

 Logger.log(result);

}




function postBlockTwitterUser(id) {

 // Encode consumer key and secret
 var tokenUrl = "https://api.twitter.com/oauth2/token";
 var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

 //  Obtain a bearer token with HTTP POST request
 var tokenOptions = {
   headers : {
     Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
   },
   method: "post",
   payload: "grant_type=client_credentials"
 };

 var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
 var parsedToken = JSON.parse(responseToken);
 var token = parsedToken.access_token;

 // Authenticate Twitter API requests with the bearer token
 var apiUrl = 'https://api.twitter.com/1.1/blocks/create.json?screen_name='+id;
 var apiOptions = {
   headers : {
     Authorization: 'Bearer ' + token
   },
   "method" : "post"
 };

 var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

 var result = "";

 if (responseApi.getResponseCode() == 200) {

   // Parse the JSON encoded Twitter API response
   var tweets = JSON.parse(responseApi.getContentText());
   return tweets.name

 }

 Logger.log(result);

}

2 个答案:

答案 0 :(得分:0)

您尝试使用仅限应用的身份验证OAuth,但您需要使用带有用户令牌的3-legged身份验证才能使用块端点。如果要发布到该端点,则需要修改代码以包含所有四个令牌(消费者密钥和密钥以及用户令牌和密钥)。

答案 1 :(得分:0)

@ andy-piper你可以修改上面的代码进行3腿认证吗?

我也在使用上述脚本,但不知道如何使用它发布推文。我有访问令牌和秘密,但不知道如何使用它。