PHPのcurl関数を用いてTwitterAPIのリクエストトークン
以下のコードで取得する際
"{"errors":[{"code":215,"message":"Bad Authentication data."}]}"
が返って来ます。
理由がわからず困っております。お願いします
PHP
1 2$params = array( 3 'oauth_callback' => '' , 4 'oauth_consumer_key' => '' , 5 'oauth_signature_method' => 'HMAC-SHA1' , 6 'oauth_timestamp' => mktime() , 7 'oauth_nonce' => md5(uniqid(rand(), true)) , 8 'oauth_version' => '1.0' 9); 10 11 $url =array( 12 'request_url' => 'https://api.twitter.com/oauth/request_token' , 13 'access_url' => 'https://api.twitter.com/oauth/access_token' , 14); 15 16 $key = array( 17 'consumer_key' => '' , 18 'consumer_secret' => '' , 19 'access_token_secret' => '', 20 ); 21 22 $request_method = 'POST'; 23 $request_url = $url['request_url']; 24 $signature_key = rawUrlEncode($key['consumer_secret']) . '&' . rawUrlEncode($key['access_token_secret']); 25 26 foreach ($params as $key => $value) { 27 if ($key !== 'oauth_callback') { 28 $params[$key] = rawUrlEncode($value); 29 }else { 30 continue; 31 } 32 } 33 ksort($params); 34 35 $request_params = http_build_query($params , ' ' , '&'); 36 $encode_url = array( 37 'request_params' => rawUrlEncode($request_params) , 38 'encoded_request_method' =>rawUrlEncode($request_method) , 39 'encoded_request_url' => rawUrlEncode($request_url) 40 ); 41$signature_data = $encode_url['encoded_request_method'] . '&' . $encode_url['encoded_request_url'] . '&' . $request_params; 42 43$hash = hash_hmac('sha1' , $signature_data , $signature_key , TRUE); 44$signature = base64_encode($hash); 45 46$params['oauth_signature'] = $signature; 47 48 $header_params = http_build_query($params , ' ' , ','); 49 $context = array( 50 'http' => array( 51 'method' => $request_method, 52 'header' => array( 53 'Authorization: OAuth' . $header_params , 54 ), 55 ) 56 ); 57 58 $curl = curl_init(); 59 curl_setopt($curl , CURLOPT_URL , $request_url); 60 curl_setopt($curl , CURLOPT_HEADER , 1); 61 curl_setopt($curl , CURLOPT_CUSTOMREQUEST , $context['http']['method']); 62 curl_setopt($curl , CURLOPT_SSL_VERIFYPEER , false); 63 curl_setopt($curl , CURLOPT_RETURNTRANSFER , true); 64 curl_setopt($curl , CURLOPT_HTTPHEADER , $context['http']['header'])); 65 curl_setopt($curl , CURLOPT_TIMEOUT , 5); 66 $post1 = curl_exec($curl); 67 $post2 = curl_getinfo($curl); 68 curl_close($curl); 69 70 $params = $params; 71 $response = substr($post1 , $post2['header_size']); 72 $header = substr($post1 , 0 , $post2['header_size']); 73 74 echo $header; 75?>
回答1件
あなたの回答
tips
プレビュー