Twitter API 尝试请求令牌时出错
Twitter API error when trying to request a token
我正在尝试执行第一步,即使用 Twitter api 端点为用户获取访问令牌。但是 request_token 端点不工作,我得到了一个通用的 500 内部服务器错误。这是我试图达到的终点:
POST https://api.twitter.com/oauth/request_token?oauth_callback=http%3A%2F%2example.com%2Fapi%2Ftwitter%2Foauth
Authorization: OAuth oauth_nonce="kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", oauth_callback="http%3A%2F%2example.com%2Fapi%2Ftwitter%2Foauth", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1630988166522", oauth_consumer_key="<MY_CONSUMER_KEY>", oauth_signature="8cVT5tNumrTit77OZkiaA1fSo7Y=", oauth_version="1.0"
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: 449c980f-eb74-41f4-a747-1fe1bd34c755
Host: api.twitter.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: guest_id=v1%3A163090645168923415; personalization_id="v1_Enx+SO8Z5+LYLTsALKSDtg=="
Content-Length: 0
HTTP/1.1 500 Internal Server Error
x-connection-hash: e6046252ae2e0f7ade3b81bd3e3aa9377e83fe0f48a686d03e14260f1f3599b6
date: Tue, 07 Sep 2021 04:16:03 GMT
server: tsa_b
content-length: 0
回调为http:///api/twitter/oauth
回调也在我的 Twitter 应用程序设置中定义....
oauth_signature 是使用以下 javascript 创建的:
function createSignature() {
var str =
"include_entities=true" +
"&oauth_callback=" + "http%3A%2F%2example.com%2Fapi%2Ftwitter%2Foauth" +
"&oauth_consumer_key=" + "<MY_CONSUMER_KEY>" +
"&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg" +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + Date.now() +
"&oauth_token=" + "<MY_APP_AUTH_ACCESS_TOKEN>" +
"&oauth_version=1.0"
;
var signature_base_string =
"POST" +
"&https%3A%2F%2Fapi.twitter.com%2F1.1%2Foauth%2Frequest_token" +
"&" + encodeURIComponent(str);
var signing_key =
"<MY_CONSUMER_SECRET>" +
"&" +
"<MY_APP_AUTH_ACCESS_SECRET>";
var hash = CryptoJS.HmacSHA1(signature_base_string, signing_key);
var base64 = hash.toString(CryptoJS.enc.Base64);
return base64;
}
我错过了什么??
谢谢!
我想通了。我在生成签名时遗漏了一些信息。它应该是这样的:
var CryptoJS = require("crypto-js");
function createSignature() {
var str =
"oauth_callback=" + encodeURIComponent("<MY_CALLBACK_URL>") +
"&oauth_consumer_key=" + "<CONSUMER_KEY>" +
"&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg" +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + Math.floor((new Date()).getTime() / 1000) +
"&oauth_token=" + "<TWITTER_ACCESS_TOKEN>" +
"&oauth_version=1.0"
;
var signature_base_string =
"POST" +
"&" + encodeURIComponent("https://api.twitter.com/oauth/request_token") +
"&" + encodeURIComponent(str);
var signing_key =
"<CONSUMER_SECRET>" +
"&" +
"<TOKEN_SECRET>";
var hash = CryptoJS.HmacSHA1(signature_base_string, signing_key);
return encodeURIComponent(hash.toString(CryptoJS.enc.Base64));
}
其他都正确...
谢谢!
我正在尝试执行第一步,即使用 Twitter api 端点为用户获取访问令牌。但是 request_token 端点不工作,我得到了一个通用的 500 内部服务器错误。这是我试图达到的终点:
POST https://api.twitter.com/oauth/request_token?oauth_callback=http%3A%2F%2example.com%2Fapi%2Ftwitter%2Foauth
Authorization: OAuth oauth_nonce="kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", oauth_callback="http%3A%2F%2example.com%2Fapi%2Ftwitter%2Foauth", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1630988166522", oauth_consumer_key="<MY_CONSUMER_KEY>", oauth_signature="8cVT5tNumrTit77OZkiaA1fSo7Y=", oauth_version="1.0"
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: 449c980f-eb74-41f4-a747-1fe1bd34c755
Host: api.twitter.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: guest_id=v1%3A163090645168923415; personalization_id="v1_Enx+SO8Z5+LYLTsALKSDtg=="
Content-Length: 0
HTTP/1.1 500 Internal Server Error
x-connection-hash: e6046252ae2e0f7ade3b81bd3e3aa9377e83fe0f48a686d03e14260f1f3599b6
date: Tue, 07 Sep 2021 04:16:03 GMT
server: tsa_b
content-length: 0
回调为http://
回调也在我的 Twitter 应用程序设置中定义....
oauth_signature 是使用以下 javascript 创建的:
function createSignature() {
var str =
"include_entities=true" +
"&oauth_callback=" + "http%3A%2F%2example.com%2Fapi%2Ftwitter%2Foauth" +
"&oauth_consumer_key=" + "<MY_CONSUMER_KEY>" +
"&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg" +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + Date.now() +
"&oauth_token=" + "<MY_APP_AUTH_ACCESS_TOKEN>" +
"&oauth_version=1.0"
;
var signature_base_string =
"POST" +
"&https%3A%2F%2Fapi.twitter.com%2F1.1%2Foauth%2Frequest_token" +
"&" + encodeURIComponent(str);
var signing_key =
"<MY_CONSUMER_SECRET>" +
"&" +
"<MY_APP_AUTH_ACCESS_SECRET>";
var hash = CryptoJS.HmacSHA1(signature_base_string, signing_key);
var base64 = hash.toString(CryptoJS.enc.Base64);
return base64;
}
我错过了什么??
谢谢!
我想通了。我在生成签名时遗漏了一些信息。它应该是这样的:
var CryptoJS = require("crypto-js");
function createSignature() {
var str =
"oauth_callback=" + encodeURIComponent("<MY_CALLBACK_URL>") +
"&oauth_consumer_key=" + "<CONSUMER_KEY>" +
"&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg" +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + Math.floor((new Date()).getTime() / 1000) +
"&oauth_token=" + "<TWITTER_ACCESS_TOKEN>" +
"&oauth_version=1.0"
;
var signature_base_string =
"POST" +
"&" + encodeURIComponent("https://api.twitter.com/oauth/request_token") +
"&" + encodeURIComponent(str);
var signing_key =
"<CONSUMER_SECRET>" +
"&" +
"<TOKEN_SECRET>";
var hash = CryptoJS.HmacSHA1(signature_base_string, signing_key);
return encodeURIComponent(hash.toString(CryptoJS.enc.Base64));
}
其他都正确...
谢谢!