php - Twitter access token request returns "Invalid or expired token" -
i using custom code connect twitter , request access token. reason when trying post "access_token" on api returns "invalid or expired token". code follows (apart few external function calls , properties should sufficient replicate error):
public function authenticate($get,$return = false) { session_start(); if (!isset($get['oauth_verifier'])){ // step 1 - request token $step1 = $this->processrequest('oauth/request_token',0,$this->pobj->getredirecturl().'?process=true'); parse_str($step1,$parts); if ($parts['oauth_callback_confirmed'] !== 'true'){ die('error process'); } $_session['tw_secret'] = $parts['oauth_token_secret']; // step 2 $url = str_replace('1.1/','',$this->api_url); header("location: {$url}oauth/authenticate?oauth_token={$parts['oauth_token']}"); } else { // step 3 $this->o_token = $get['oauth_token']; $this->o_secret = $_session['tw_secret']; $content['oauth_verifier'] = $get['oauth_verifier']; $step3 = $this->processrequest('oauth/access_token',1,null,$content,'array'); } } // https://dev.twitter.com/docs/auth/creating-signature private function generatesignature($oauth,$fullurl,$http_method,$content){ // take params url $main_url = explode('?',$fullurl); // split content $contents = explode('&',$content); $urls = array_merge(explode('&',$main_url[1]),$contents); foreach ($urls $param){ $bits = explode('=',$param); if (strlen($bits[0])){ $oauth[$bits[0]] = rawurlencode($bits[1]); } } ksort($oauth); $string = http_build_query($oauth); $new_string = strtoupper($http_method).'&'.urlencode($main_url[0]).'&'.urlencode(urldecode($string)); // request_token request doesn't need o_secret because doesn't have one! $sign_key = strstr($fullurl,'request_token') ? $this->c_secret.'&' : $this->c_secret.'&'.$this->o_secret; return urlencode(base64_encode(hash_hmac('sha1',$new_string,$sign_key,true))); } public function processrequest($in_url,$test = false,$callback = null,$content = null, $content_type = 'json',$form_content_type = 'application/x-www-form-urlencoded'){ $method = 'get'; // twitter still uses oauth1 (which pain) $oauth = array( 'oauth_consumer_key'=>$this->c_key, 'oauth_nonce'=>$this->random(32), 'oauth_signature_method'=>'hmac-sha1', 'oauth_timestamp'=>time(), 'oauth_token'=>$this->o_token, 'oauth_version'=>'1.0' ); $url = $this->api_url.$in_url; if (strlen($callback)){ $oauth['oauth_callback'] = urlencode(urldecode($callback)); unset($oauth['oauth_token']); $method = 'post'; $url = str_replace('1.1/','',$url); } if (is_array($content) || strlen($content)){ $method = 'post'; } $oauth['oauth_signature'] = $this->generatesignature($oauth,$url,$method,''); ksort($oauth); foreach ($oauth $k=>$v){ $auths[] = $k.'="'.$v.'"'; } $stream = array('http' => array( 'method' => $method, 'ignore_errors'=>true, // http://php.net/manual/en/context.http.php - otherwise browser returns error not error content 'follow_location'=>false, 'max_redirects'=>0, 'header'=> array( 'content-type: '.$form_content_type, 'authorization: oauth '.implode(', ',$auths), 'connection: close' ) ) ); if (is_array($content)){ $content = $content_type == 'json' ? json_encode($content) : http_build_query($content); /* foreach ($content $k=>$v){ $strs[] = "$k=".urlencode(urldecode($v)); } // keep things simple $content = 'status=hello%20ladies%20%2b%20gentlemen%2c%20a%20signed%20oauth%20request%21';*/ } if (!is_null($content)){ $stream['http']['content'] = $content; } // tell streams make request // invalid key or 401 error tends suggest incorrect signing key / signature $response = file_get_contents($url, false, stream_context_create($stream)); if ($test){ print'<pre>';print_r($oauth);print'</pre>'; print'<pre>';print_r($stream);print'</pre>'; //echo $callback.'<br>'; echo $url.'<br>'; //print'<pre>';print_r($http_response_header);print'</pre>'; print'<pre>[';print_r($response);print']</pre>'; } if (!is_object(json_decode($response))){ // content supplied not json - return return $response; } else { $response = json_decode($response); } return $this->pobj->convertobjecttoarray($response); }
the reason problems are:
1) version of twitter api should not included 2) post missing oauth_verifier in signature
thanks twitteroauth providing guiding light.
Comments
Post a Comment