Twitter REST API oauth';215错误的身份验证数据';使用fetch-in-react-native

Twitter REST API oauth';215错误的身份验证数据';使用fetch-in-react-native,rest,twitter,react-native,oauth,fetch,Rest,Twitter,React Native,Oauth,Fetch,我正在尝试构建一个react本机应用程序来显示用户的twitter时间线,但无法访问twitter REST api,因为我得到了一个 215错误的身份验证数据 错误 我已经正确地登录了用户,获得了所有的访问令牌,并使用fetch发出请求。 我还验证了我所有的密钥和令牌都是正确的,但我仍然无法找出为什么会出现这个错误,我的代码如下所示 有谁能告诉我如何调试这个或告诉我我的代码有什么问题吗? 谢谢 代码: let header = this._buildRequestHeader(twitter_

我正在尝试构建一个react本机应用程序来显示用户的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(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;
}


但是我得到了一个错误的身份验证数据。有人能告诉你吗?

你试过使用这个软件包吗

让我知道这有什么帮助

致以最良好的祝愿。
Maciej Adamczewski

您尝试过使用此软件包吗

让我知道这有什么帮助

致以最良好的祝愿。
Maciej Adamczewski

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

我发现了问题所在:我使用的是base-64编码。
正如Maciej Adamczewski所指出的,标题字符串中有一个不必要的空格

感谢您的参考,但该库似乎只适用于android…我正在为ios构建第一个您建议的第二个软件包实际上是我用于登录的,但这就是它提供的所有功能:登录,它不能帮助您进行REST API请求,谢谢。谢谢您的参考,但该库似乎只适用于android…我正在为ios First构建您建议的第二个软件包实际上是我用于登录的,但这就是它提供的所有功能:登录,它不能帮助您进行REST API请求,谢谢。'Authorization':''+header为什么在header之前有空白?我意识到这也是问题的原因。谢谢。'Authorization':''+header为什么在header之前有空白?我意识到这也是问题的原因。谢谢。:-)很高兴在这里我能帮忙:-)很高兴在这里我能帮忙