首页 > 解决方案 > 消息说 [Ljava.lang.Object] 被传递代替值

问题描述

我正在尝试从 Twitter API V2 收集推文:
https ://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-tweets

我用来将推文值数据发送到 Google 表格单元格的脚本是:

function TwitterTest() {
  var string_Screen_name = "1310800524619386880";
  var string_Consumer_key = "AAAAAAAAAAAAAAAAAAAAAAAAAA";
  var string_Consumer_secret = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
  
  var tokenUrl = "https://api.twitter.com/oauth2/token";
  var tokenCredential = Utilities.base64EncodeWebSafe(string_Consumer_key + ":" + string_Consumer_secret);
  
  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;
  var apiUrl = "";
  var responseApi = "";
  
  var apiOptions = {
    headers : {
      Authorization: 'Bearer ' + token
    },
    "method" : "get"
  };
  
  var apiUrl = 'https://api.twitter.com/2/users/'+ string_Screen_name +'/tweets?expansions=attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,reply_settings,source,text,withheld&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type&poll.fields=duration_minutes,end_datetime,id,options,voting_status&media.fields=duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics,non_public_metrics,organic_metrics,promoted_metrics';

  responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);
  
  var obj_data = JSON.parse(responseApi.getContentText());
  
   SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tweets").getRange("A3").setValue(obj_data.data);
}

得到的结果:

{attachments={media_keys=[Ljava.lang.Object;@1152e2a4}, entities={urls=[Ljava.lang.Object;@1373921e}, possibly_sensitive=false, conversation_id=1402411015724175370, public_metrics={like_count=1, reply_count=0, quote_count=0, retweet_count=0}, created_at=2021-06-08T23:43:36.000Z, source=Twitter Web App, id=1402411015724175370, text=ALERT: New high roller bet posted!
A parlay bet has been placed for $5,403.62 to win $6,241.18.
To view this bet or copy it https:// t.co /lrBXjHN0At https:// t.co /nBPMsgXI2g, author_id=1310800524619386880, lang=en, reply_settings=everyone}

具体urls结果是:

urls=[Ljava.lang.Object;@1373921e}

中的预期结果urls将类似于(从 Twitter API V2 网站收集的示例):

在此处输入图像描述

标签: javascriptgoogle-apps-scriptgoogle-sheetstwitter

解决方案


在您的情况下,以下修改是您期望的结果吗?

从:

SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tweets").getRange("A3").setValue(obj_data.data);

至:

SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tweets").getRange("A3").setValue(JSON.stringify(obj_data.data));

推荐阅读