Twitter API返回错误215,错误验证数据

Dip*_*ani 108 php api twitter

我试图跟随Twitter的API来获取用户的关注者列表.

http://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=username

我收到此错误消息作为回应.

{
    code = 215;
    message = "Bad Authentication data";
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法找到与此错误代码相关的文档.有人对这个错误有任何想法吗?

A B*_*ker 25

没有任何其他php文件的非常简洁的代码包括oauth等.请注意要获得以下密钥,您需要使用https://dev.twitter.com注册并创建应用程序.

<?php
$token = 'YOUR_TOKEN';
$token_secret = 'YOUR_TOKEN_SECRET';
$consumer_key = 'CONSUMER_KEY';
$consumer_secret = 'CONSUMER_SECRET';

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path

$query = array( // query parameters
    'screen_name' => 'twitterapi',
    'count' => '5'
);

$oauth = array(
    'oauth_consumer_key' => $consumer_key,
    'oauth_token' => $token,
    'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
    'oauth_timestamp' => time(),
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_version' => '1.0'
);

$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);

$arr = array_merge($oauth, $query); // combine the values THEN sort

asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)

// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));

$url = "https://$host$path";

// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);

// same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

// generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);
$url=str_replace("&amp;","&",$url); //Patch by @Frewuill

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it

// also not necessary, but twitter's demo does this too
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);

// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

// if you're doing post, you need to skip the GET building above
// and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);


foreach ($twitter_data as &$value) {
   $tweetout .= preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $value->text);
   $tweetout = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $tweetout);
   $tweetout = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $tweetout);
}

echo $tweetout;

?>
Run Code Online (Sandbox Code Playgroud)

问候


Pav*_*vel 11

到目前为止我找到的唯一解决方案是:

  • 在Twitter开发人员面板中创建应用
  • 使用您的应用程序(或您在用户帐户中的应用程序)授权用户并保存Twitter提供给您的"oauth_token"和"oauth_token_secret".使用TwitterOAuth库,这非常简单,请参阅库中的示例.
  • 使用此令牌,您可以代表用户进行经过身份验证的请求.您可以使用相同的库来完成.

    // Arguments 1 and 2 - your application static tokens, 2 and 3 - user tokens, received from Twitter during authentification  
    $connection = new TwitterOAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, $tokens['oauth_token'], $tokens['oauth_token_secret']);  
    $connection->host = 'https://api.twitter.com/1.1/'; // By default library uses API version 1.  
    $friendsJson = $connection->get('/friends/ids.json?cursor=-1&user_id=34342323');  
    
    Run Code Online (Sandbox Code Playgroud)

这将返回用户的朋友列表.


Dan*_*ari 7

找到解决方案 - 使用亚伯拉罕TwitterOAuth库.如果您使用的是较旧的实现,则应在实例化新的TwitterOAuth对象后添加以下行:

$connection->host = "https://api.twitter.com/1.1/";
$connection->ssl_verifypeer = TRUE;
$connection->content_type = 'application/x-www-form-urlencoded';
Run Code Online (Sandbox Code Playgroud)

现在,前两行记录在亚伯拉罕图书馆自述文件中,但第三行不是.还要确保你的oauth_version仍然是1.0.

这是我的代码,用于从"users/show"获取所有用户数据和新认证的用户,并返回用户的全名和用户图标1.1 - 以下代码在认证回调文件中实现:

session_start();
require ('twitteroauth/twitteroauth.php');
require ('twitteroauth/config.php');

$consumer_key = '****************';
$consumer_secret = '**********************************';

$to = new TwitterOAuth($consumer_key, $consumer_secret);

$tok = $to->getRequestToken('http://exampleredirect.com?twitoa=1');

$token = $tok['oauth_token'];
$secret = $tok['oauth_token_secret'];

//save tokens to session
$_SESSION['ttok'] = $token;
$_SESSION['tsec'] = $secret;

$request_link = $to->getAuthorizeURL($token,TRUE);

header('Location: ' . $request_link);
Run Code Online (Sandbox Code Playgroud)

然后,以下代码在身份验证和令牌请求之后进行重定向

if($_REQUEST['twitoa']==1){
    require ('twitteroauth/twitteroauth.php');
    require_once('twitteroauth/config.php');
    //Twitter Creds
    $consumer_key = '*****************';
    $consumer_secret = '************************************';

    $oauth_token = $_GET['oauth_token']; //ex Request vals->http://domain.com/twitter_callback.php?oauth_token=MQZFhVRAP6jjsJdTunRYPXoPFzsXXKK0mQS3SxhNXZI&oauth_verifier=A5tYHnAsbxf3DBinZ1dZEj0hPgVdQ6vvjBJYg5UdJI

    $ttok = $_SESSION['ttok'];
    $tsec = $_SESSION['tsec'];

    $to = new TwitterOAuth($consumer_key, $consumer_secret, $ttok, $tsec);
    $tok = $to->getAccessToken();
    $btok = $tok['oauth_token'];
    $bsec = $tok['oauth_token_secret'];
    $twit_u_id = $tok['user_id'];
    $twit_screen_name = $tok['screen_name'];

    //Twitter 1.1 DEBUG
    //print_r($tok);
    //echo '<br/><br/>';
    //print_r($to);
    //echo '<br/><br/>';
    //echo $btok . '<br/><br/>';
    //echo $bsec . '<br/><br/>';
    //echo $twit_u_id . '<br/><br/>';
    //echo $twit_screen_name . '<br/><br/>';

    $twit_screen_name=urlencode($twit_screen_name);
    $connection = new TwitterOAuth($consumer_key, $consumer_secret, $btok, $bsec);
    $connection->host = "https://api.twitter.com/1.1/";
    $connection->ssl_verifypeer = TRUE;
    $connection->content_type = 'application/x-www-form-urlencoded';
    $ucontent = $connection->get('users/show', array('screen_name' => $twit_screen_name));

    //echo 'connection:<br/><br/>';
    //print_r($connection);
    //echo '<br/><br/>';
    //print_r($ucontent);

    $t_user_name = $ucontent->name;
    $t_user_icon = $ucontent->profile_image_url;

    //echo $t_user_name.'<br/><br/>';
    //echo $t_user_icon.'<br/><br/>';
}
Run Code Online (Sandbox Code Playgroud)

我花了太长时间才弄清楚这一点.希望这有助于某人!


chr*_*her 5

更新: Twitter API 1现已弃用.参考上面的答案.

Twitter 1.1无法使用该语法(当我写这个答案时).需要为1,而不是1.1.这将有效:

http://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=username

  • 从2013年3月起,版本1将在6个月内弃用,因此我将使用1.1版 (7认同)
  • Twitter REST API v1不再处于活动状态.请迁移到API v1.1.https://dev.twitter.com/docs/api/1.1/overview. (4认同)
  • 是的,这是正确的.但那是因为Twitter的文档建议我这样做.(https://dev.twitter.com/docs/api/1/get/followers/ids).他们说版本1已被弃用,我需要转向1.1.版本1适用于此Web服务.但我很困惑为什么1.1不适合我? (3认同)
  • Twitter API 1已弃用 (2认同)

小智 5

其中的url /1.1/是正确的,它是新的Twitter API版本1.1.

但是您需要一个应用程序并使用oAuth授权您的应用程序(和用户).

Twitter Developers文档网站上阅读更多相关信息 :)

  • 引用文档站点并不能真正回答这个问题. (140认同)
  • @moluv00 OP说:"我似乎无法找到与此错误代码相关的文档." (4认同)
  • 你为什么发布一般链接.这不是答案. (4认同)

Jon*_*aar 5

Gruik的答案在下面的帖子中为我工作.

{摘录| Zend_Service_Twitter - 准备好API v1.1 }

使用ZF 1.12.3,解决方法是在oauthOptions选项中传递consumerKey和consumerSecret,而不是直接在选项中传递.

    $options = array(
        'username' => /*...*/,
        'accessToken' => /*...*/,
        'oauthOptions' => array(
            'consumerKey' => /*...*/,
            'consumerSecret' => /*...*/,
        )
    );
Run Code Online (Sandbox Code Playgroud)