【问题标题】:Twitter REST API oauth '215 Bad Authentication data' using fetch in react-nativeTwitter REST API oauth '215 Bad Authentication data' 在 react-native 中使用 fetch
【发布时间】:2017-12-17 17:08:11
【问题描述】:

我正在尝试构建一个反应原生应用程序来显示用户的 Twitter 时间线,但我无法访问 twitter REST api,因为我得到了一个

215 错误的身份验证数据

错误。

我已正确登录用户,获得所有访问令牌,并使用 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 数据。有人可以请教吗?

【问题讨论】:

  • 'Authorization': ' '+header 为什么 Header 前面有这个空白?
  • 我意识到这也导致了问题。谢谢。

标签: rest twitter react-native oauth fetch


【解决方案1】:

我发现问题出在哪里:我使用的HMAC-SHA1 function已经在为我进行 base-64 编码。 正如 Maciej Adamczewski 指出的那样,标题字符串中有一个不必要的空格

【讨论】:

  • :-) 很高兴我能帮上忙
【解决方案2】:

你试过用这个包吗?

https://www.npmjs.com/package/react-native-twitter

https://github.com/GoldenOwlAsia/react-native-twitter-signin

让我知道它是否有帮助。

最好的问候。 马切伊·亚当切夫斯基

【讨论】:

  • 感谢您的参考,但该库似乎仅适用于 android....我首先为 ios 构建
  • 您建议的第二个包实际上是我用于登录的包,但这就是它提供的所有功能:登录,它不能帮助您发出 REST API 请求,谢谢。
猜你喜欢
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 2017-07-10
相关资源
最近更新 更多