php - how to solve error {"message":"Could not authenticate you","code":32} -
i want fetch public posts twitter. create app in twitter , gather consumer key (api key)
,consumer secret (api secret)
,access level,access token,access token secret,access level , write program. @ run time shows error
{"message":"could not authenticate you","code":32}
.
create class twitterapi keys private set post. create function setgetfield()
public function setgetfield($string) { if (!is_null($this->getpostfields())) { throw new exception('you can choose or post fields.'); } $search = array('#', ',', '+', ':'); $replace = array('%23', '%2c', '%2b', '%3a'); $string = str_replace($search, $replace, $string); $this->getfield = $string; return $this; }
create function getgetfields() collect field twitter.
public function getgetfields() { return $this->getfield; }
to build oauth write buildouath fuction
public function buildoauth($url, $requestmethod) { if (!in_array(strtolower($requestmethod), array('post', 'get'))) { throw new exception('request method must either post or get'); } $consumer_key = $this->consumer_key; $consumer_secret = $this->consumer_secret; $oauth_access_token = $this->oauth_access_token; $oauth_access_token_secret = $this->oauth_access_token_secret; $oauth = array( 'oauth_consumer_key' => $consumer_key, 'oauth_nonce' => time(), 'oauth_signature_method' => 'hmac-sha1', 'oauth_token' => $oauth_access_token, 'oauth_timestamp' => time(), 'oauth_version' => '1.0' ); $getfield = $this->getgetfield(); if (!is_null($getfield)) { $getfields = str_replace('?', '', explode('&', $getfield)); foreach ($getfields $g) { $split = explode('=', $g); $oauth[$split[0]] = $split[1]; } } $base_info = $this->buildbasestring($url, $requestmethod, $oauth); $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret); $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true)); $oauth['oauth_signature'] = $oauth_signature; $this->url = $url; $this->oauth = $oauth; return $this; }
to generate authorisation header
private function buildauthorizationheaders($oauth) { $return = 'authorization: oauth '; $values = array(); foreach($oauth $key => $value) { $values[] = "$key=\"" . rawurlencode($value) . "\""; } $return .= implode(', ', $values); return $return; }
how solve difficulty
Comments
Post a Comment