带有Node.js twitter api GET请求的状态码400?

时间:2019-09-21 23:00:44

标签: node.js twitter

这是我的设置:

  const method = "GET";
  const baseUrl = "https://api.twitter.com/1.1/statuses/user_timeline.json";
  const percentEncodedBaseUrl = encodeURIComponent(baseUrl);

  const count = 200;
  const include_rts = 1;
  const oAuthConsumerKey = CONFIG.CONSUMER_KEY;
  const oAuthNonce = crypto.randomBytes(32).toString("base64");
  const oAuthSignatureMethod = "HMAC-SHA1";
  const oAuthTimeStamp = moment()
    .utc()
    .unix();
  const oAuthToken = CONFIG.AUTH_TOKEN;
  const oAuthVersion = "1.0";
  const userId = url.split("/")[3];

  const oAuthConsumerSecret = CONFIG.CONSUMER_SECRET;
  const signingKey = `${oAuthConsumerSecret}&`;

  const paramString = `count=${count}&include_rts=${include_rts}&oauth_consumer_key=${oAuthConsumerKey}&oauth_nonce=${oAuthNonce}&oauth_signature_method=${oAuthSignatureMethod}&oauth_timestamp=${oAuthTimeStamp}&oath_token=${oAuthToken}&oauth_version=${oAuthVersion}`;
  const percentEncodedParamString = encodeURIComponent(paramString);

  const signatureBaseString = `${method}&${percentEncodedBaseUrl}&${percentEncodedParamString}`;

  const oAuthSignature = crypto
    .createHmac("sha1", signingKey)
    .update(signatureBaseString, "binary")
    .digest("base64");

  const getActivity = `https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=${userId}&include_rts=${include_rts}&count=${count}`;
  const options = {
    headers: {
      withCredentials: true,
      credentials: "include",
      Authorization: `OAuth oauth_consumer_key=${oAuthConsumerKey},oauth_token=${oAuthToken},oauth_signature_method=${oAuthSignatureMethod},oauth_signature=${oAuthSignature},oauth_timestamp="${oAuthTimeStamp}",oauth_nonce="${oAuthNonce}",oauth_version=${oAuthVersion}`
    }
  };

  const results = await fetch(getActivity, options);

我已通过向Postman发出成功请求来验证非生成的值以及我正在使用的生成的随机数有效。我相信我已经遵循了twitter api文档到T了。我的签名类似于twitter示例签名,并且我的值中肯定没有前导/后缀空格。

不过,通过此设置,我得到了Status 400 Bad Requesterror: Bad Authentication data的帮助。

有什么想法吗?

0 个答案:

没有答案