Twitter REST API oauth '215 Bad Authentication data' 在 react-native 中使用 fetch
Twitter REST API oauth '215 Bad Authentication data' using fetch in react-native
我正在尝试构建一个 React 本机应用程序来显示用户的推特时间轴,但无法访问推特 REST api,因为我收到
215 Bad Authentication data
错误。
我已正确登录用户,获得所有访问令牌,并使用 fetch 发出请求。
我还验证了我所有的密钥和令牌都是正确的,但我仍然无法弄清楚为什么会出现此错误,我的代码在下面可用;
任何人都可以告诉我如何调试它或告诉我我的代码有什么问题吗?
谢谢。
代码:
let header = this._buildRequestHeader(twitter_token, twitter_tokenSecret);
console.log(header);
fetch('https://api.twitter.com/1.1/statuses/home_timeline.json', {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': ' '+header
}
}).then((response) => response.json())
.then((json) => {
console.log(json);
})
_getBaseString:
_getBaseString(method, url, parameter_string){
return method+'&'+
encodeURIComponent(url)+'&'+encodeURIComponent(parameter_string);
}
_getSignature:
_getSignature(user_auth_token, accesstoken_secret, data){
// let signing_key = encodeURIComponent(Constants.TWITTER_CONSUMER_SECRET)+'&'+
// encodeURIComponent(Constants.ACCESS_TOKEN_SECRET);
let signing_key = encodeURIComponent(Constants.TWITTER_CONSUMER_SECRET)+'&'+
encodeURIComponent(accesstoken_secret);
console.log('signing data');
console.log(data);
return this.b64EncodeUnicode(hmacsha1(signing_key, data));
}
_buildRequestHeader:
_buildRequestHeader(user_auth_token, accesstoken_secret){
// https://dev.twitter.com/oauth/overview/creating-signatures
// https://dev.twitter.com/oauth/overview/authorizing-requests
let include_entities_key = encodeURIComponent('include_entities');
let include_entities_val = encodeURIComponent('false');
let oauth_consumer_key_key = encodeURIComponent('oauth_consumer_key');
let oauth_consumer_key_val = encodeURIComponent(Constants.TWITTER_COMSUMER_KEY);
let oauth_nonce_key = encodeURIComponent('oauth_nonce');
let oauth_nonce_val = encodeURIComponent(this._getNonce());
let oauth_signature_method_key = encodeURIComponent('oauth_signature_method');
let oauth_signature_method_val = encodeURIComponent('HMAC-SHA1');
let oauth_timestamp_key = encodeURIComponent('oauth_timestamp');
var val = Date.now() / 1000;
console.log(val);
console.log('parse'+parseInt(val));
let oauth_timestamp_val = encodeURIComponent(parseInt(val));
let oauth_token_key = encodeURIComponent('oauth_token');
// let oauth_token_val = encodeURIComponent(Constants.ACCESS_TOKEN);
let oauth_token_val = encodeURIComponent(user_auth_token);
let oauth_version_key = encodeURIComponent('oauth_version');
let oauth_version_val = encodeURIComponent('1.0');
// let parameter_string = include_entities_key+'='+include_entities_val+'&'+
let parameter_string = oauth_consumer_key_key+'='+oauth_consumer_key_val+'&'+
oauth_nonce_key+'='+oauth_nonce_val+'&'+
oauth_signature_method_key+'='+oauth_signature_method_val+'&'+
oauth_timestamp_key+'='+oauth_timestamp_val+'&'+
oauth_token_key+'='+oauth_token_val+'&'+
oauth_version_key+'='+oauth_version_val;
let data = this._getBaseString('GET', 'https://api.twitter.com/1.1/statuses/home_timeline.json',
parameter_string);
let signature = this._getSignature(user_auth_token, accesstoken_secret, data)
console.log('signature'+signature);
// 1499887682711
// 1318622958
let oauth_signature_key = encodeURIComponent('oauth_signature');
let oauth_signature_val = encodeURIComponent(signature);
let request_header_string = 'OAuth '+
oauth_consumer_key_key+'="'+oauth_consumer_key_val+'", '+
oauth_nonce_key+'="'+oauth_nonce_val+'", '+
oauth_signature_key+'="'+oauth_signature_val+'", '+
oauth_signature_method_key+'="'+oauth_signature_method_val+'", '+
oauth_timestamp_key+'="'+oauth_timestamp_val+'", '+
oauth_token_key+'="'+oauth_token_val+'", '+
oauth_version_key+'="'+oauth_version_val+'"';
return request_header_string;
}
但是我得到了 215 Bad Authentication 数据。有人可以请教吗?
你试过使用这个包吗?
https://www.npmjs.com/package/react-native-twitter
或
https://github.com/GoldenOwlAsia/react-native-twitter-signin
让我知道它是否有帮助。
此致。
Maciej Adamczewski
我知道问题出在哪里了:我使用的 HMAC-SHA1 function 已经为我进行了 base-64 编码。
正如 Maciej Adamczewski 指出的那样,header 字符串
中有一个不必要的空格
我正在尝试构建一个 React 本机应用程序来显示用户的推特时间轴,但无法访问推特 REST api,因为我收到
215 Bad Authentication data
错误。
我已正确登录用户,获得所有访问令牌,并使用 fetch 发出请求。 我还验证了我所有的密钥和令牌都是正确的,但我仍然无法弄清楚为什么会出现此错误,我的代码在下面可用;
任何人都可以告诉我如何调试它或告诉我我的代码有什么问题吗? 谢谢。
代码:
let header = this._buildRequestHeader(twitter_token, twitter_tokenSecret);
console.log(header);
fetch('https://api.twitter.com/1.1/statuses/home_timeline.json', {
method: 'GET',
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': ' '+header
}
}).then((response) => response.json())
.then((json) => {
console.log(json);
})
_getBaseString:
_getBaseString(method, url, parameter_string){
return method+'&'+
encodeURIComponent(url)+'&'+encodeURIComponent(parameter_string);
}
_getSignature:
_getSignature(user_auth_token, accesstoken_secret, data){
// let signing_key = encodeURIComponent(Constants.TWITTER_CONSUMER_SECRET)+'&'+
// encodeURIComponent(Constants.ACCESS_TOKEN_SECRET);
let signing_key = encodeURIComponent(Constants.TWITTER_CONSUMER_SECRET)+'&'+
encodeURIComponent(accesstoken_secret);
console.log('signing data');
console.log(data);
return this.b64EncodeUnicode(hmacsha1(signing_key, data));
}
_buildRequestHeader:
_buildRequestHeader(user_auth_token, accesstoken_secret){
// https://dev.twitter.com/oauth/overview/creating-signatures
// https://dev.twitter.com/oauth/overview/authorizing-requests
let include_entities_key = encodeURIComponent('include_entities');
let include_entities_val = encodeURIComponent('false');
let oauth_consumer_key_key = encodeURIComponent('oauth_consumer_key');
let oauth_consumer_key_val = encodeURIComponent(Constants.TWITTER_COMSUMER_KEY);
let oauth_nonce_key = encodeURIComponent('oauth_nonce');
let oauth_nonce_val = encodeURIComponent(this._getNonce());
let oauth_signature_method_key = encodeURIComponent('oauth_signature_method');
let oauth_signature_method_val = encodeURIComponent('HMAC-SHA1');
let oauth_timestamp_key = encodeURIComponent('oauth_timestamp');
var val = Date.now() / 1000;
console.log(val);
console.log('parse'+parseInt(val));
let oauth_timestamp_val = encodeURIComponent(parseInt(val));
let oauth_token_key = encodeURIComponent('oauth_token');
// let oauth_token_val = encodeURIComponent(Constants.ACCESS_TOKEN);
let oauth_token_val = encodeURIComponent(user_auth_token);
let oauth_version_key = encodeURIComponent('oauth_version');
let oauth_version_val = encodeURIComponent('1.0');
// let parameter_string = include_entities_key+'='+include_entities_val+'&'+
let parameter_string = oauth_consumer_key_key+'='+oauth_consumer_key_val+'&'+
oauth_nonce_key+'='+oauth_nonce_val+'&'+
oauth_signature_method_key+'='+oauth_signature_method_val+'&'+
oauth_timestamp_key+'='+oauth_timestamp_val+'&'+
oauth_token_key+'='+oauth_token_val+'&'+
oauth_version_key+'='+oauth_version_val;
let data = this._getBaseString('GET', 'https://api.twitter.com/1.1/statuses/home_timeline.json',
parameter_string);
let signature = this._getSignature(user_auth_token, accesstoken_secret, data)
console.log('signature'+signature);
// 1499887682711
// 1318622958
let oauth_signature_key = encodeURIComponent('oauth_signature');
let oauth_signature_val = encodeURIComponent(signature);
let request_header_string = 'OAuth '+
oauth_consumer_key_key+'="'+oauth_consumer_key_val+'", '+
oauth_nonce_key+'="'+oauth_nonce_val+'", '+
oauth_signature_key+'="'+oauth_signature_val+'", '+
oauth_signature_method_key+'="'+oauth_signature_method_val+'", '+
oauth_timestamp_key+'="'+oauth_timestamp_val+'", '+
oauth_token_key+'="'+oauth_token_val+'", '+
oauth_version_key+'="'+oauth_version_val+'"';
return request_header_string;
}
但是我得到了 215 Bad Authentication 数据。有人可以请教吗?
你试过使用这个包吗?
https://www.npmjs.com/package/react-native-twitter
或
https://github.com/GoldenOwlAsia/react-native-twitter-signin
让我知道它是否有帮助。
此致。 Maciej Adamczewski
我知道问题出在哪里了:我使用的 HMAC-SHA1 function 已经为我进行了 base-64 编码。 正如 Maciej Adamczewski 指出的那样,header 字符串
中有一个不必要的空格