I am getting "Could not authenticate with OAuth" error when i pass parameters to the Twitter API. if i don't pass any parameter then it works fine.
Can anybody look into this issue and provide me the solution.
My OAuth request array is as below
Array
(
[method] => GET
[uri] => Array
(
[host] => api.twitter.com
[path] => 1/direct_messages.json
)
[auth] => Array
(
[method] => OAuth
[oauth_token] => 605267577-YwEtkLXgDZcXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[oauth_token_secret] => p2SKUjaRz7oUqW8jmXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[oauth_consumer_key] => PAu4LlcETXXXXXXXXXXXXX
[oauth_consumer_secret] => OA2ISmIYMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
)
[body] => Array
(
[count] => 200
)
[header] => Array
(
[Authorization] => OAuth oauth_version="1.0",oauth_signature_method="HMAC-SHA1",oauth_token="605267577-YwEtkLXgDZcLaFIZHdJxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",oauth_consumer_key="PAu4LlcETSxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",oauth_nonce="616caa42455f6a490f7fce089fdb56b5",oauth_timestamp="1342592116",oauth_signature="B2Dr4CioXL55fwxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)
)
i passed the "count" parameter and this is response that i got
Array
(
[error] => Could not authenticate with OAuth.
[request] => /1/direct_messages.json
)
Thanks in advance,
Intekhab
You do not authenticate correctly. You have to follow this process : https://dev.twitter.com/docs/auth/authorizing-request
I had the same problem until I found out that API needs to have that parameter in oauth array and in URL query.
Related
I want to use the Twitter Streaming Api with the filter query.
And following the GuzzleHttp documentation I'm accessing like this:
<?php
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
$stack = HandlerStack::create();
$middleware = new Oauth1([
'consumer_key' => 'my_key',
'consumer_secret' => 'my_secret',
'token' => 'my_token',
'token_secret' => 'my_token_secret'
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => 'https://stream.twitter.com/1.1/',
'handler' => $stack,
'auth' => 'oauth'
]);
$client->post('statuses/filter.json', [
'form_params' => [
'track' => 'some_word_to_track',
],
]);
But when executing this code, I'm getting the following exception:
PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException:
Client error: 'POST https://stream.twitter.com/statuses/filter.json'
resulted in a '401 Authorization Required' response
in /Users/.../vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:111
Conclusion:
Is it because I cannot use GuzzleHttp's Oauth1 Subscriber with
Twitter streaming API?
Or is it because I missing something on the
authentication?
In a Google Oauth2 implementation I'm trying to exchange an authorization code for tokens using a guzzle call.
The following guzzle call works fine and returns the expected values:
$result = $this->client->post(
'https://www.googleapis.com/oauth2/v3/token?code=<authorization_code>&redirect_uri=<redirect_uri>&client_id=<client_id>&client_secret=<client_secret>&grant_type=authorization_code')
->getBody()->getContents();
However this seems a dirty way to mount the post request.
I've tried the following way in order make it cleaner:
$result = $this->client->post(
'https://www.googleapis.com/oauth2/v3/token',
[
'query' =>
[
'code' => <authorization_code>,
'redirect_uri' => <redirect_uri>,
'client_id' => <client_id>,
'client_secret' => <client_secret>
'grant_type' => 'authorization_code',
]
]
)->getBody()->getContents();
However this second call generates a Malformed Json error message.
Any idea what I might be doing wrong or how can I debug what final url is being generated in the example above?
I tried without code parameter and it worked.
$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.googleapis.com/oauth2/v3/token', [
'query' => [
'client_id' => '...apps.googleusercontent.com',
'client_secret' => 'secret',
'refresh_token' => 'token',
'grant_type' => 'refresh_token'
]
]);
$token = $response->getBody()->getContents()
Have you tried using arrays and http://php.net/json_encode
The problem arises when I get the code and makes a request for a token. For authorization using code that works fine on other projects. I check Credentials in developer console and my config file. Attempts to connect the authorization from other sites and other accounts yielded nothing. I checked redirects, headers, but found nothing.
$request = Google_Client::$io->makeRequest(new Google_HttpRequest('https://accounts.google.com/o/oauth2/token', 'POST', array(), array(
'code' => $_GET['code'],
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUri,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret
)));
This request return "Error fetching OAuth2 access token, message: 'invalid_grant'"
Dear Google Engineers,
I keep trying to insert a customer and receive the following response:
[error] => Array
(
[errors] => Array
(
[0] => Array
(
[domain] => global
[reason] => duplicate
[message] => Resource already exists
)
)
[code] => 409
[message] => Resource already exists
)
I then go about attempting to retrieve the customer resource that supposedly exists via a GET request to https://www.googleapis.com/apps/reseller/v1/customers/{customerId} which fails to find the resource:
[error] => Array
(
[errors] => Array
(
[0] => Array
(
[domain] => global
[reason] => notFound
[message] => Not Found
)
)
[code] => 404
[message] => Not Found
)
Either I've uncovered a bug or the error response from POST https://www.googleapis.com/apps/reseller/v1/customers/ isn't nearly detailed enough for me to diagnose.
I've tried variations of the required fields with random values (addressLine1, alternateEmail, phoneNumber). I've also logged in to the Reseller CPanel and no matching customer exists for the domain.
If Google engineers care to try and diagnose internally, the customerId in question is ruby-stagingpop.co. You can email me to find out my Reseller account if necessary. Info in my profile.
I am working with YouTube APIs for my college project, and I keep getting an error. Here I send them to the authorisation page to log in, when they allow access it sends the $_GET['code'] string back. Then I send this along with some other data and it should send back a JSON object. Instead I am just getting
Warning: file_get_contents(https://accounts.google.com/o/oauth2/token)
[function.file-get-contents]: failed to open stream: HTTP request
failed! HTTP/1.0 400 Bad Request in http://www.example.net/callback.php on line 27
I have replaced my domain with example.net just for security
urlencode($_GET['code']),
'client_id' => urlencode('111522767640.apps.googleusercontent.com '),
'client_secret' => urlencode('secret'),
'redirect_uri' => urlencode('http://example.net/callback.php'),
'grant_type' => urlencode('authorization_code')
)
);
$params =
array('http' =>
array(
'method' => 'POST /o/oauth2/token HTTP/1.1',
'header' => 'Host: accounts.google.com\r\n'.
'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($params);
$result = file_get_contents('https://accounts.google.com/o/oauth2/token', false,$context);
var_dump($_SESSION);
var_dump($result);
}
else //If code isnt set, user must have come here erroniously or has denied access to this program
{
//header( 'Location: www.example.net/error.php' ) ;
}
?>
file_get_contents is going to make a GET request to the url specified, but oauth2/token needs a POST request.
See reference Google OAuth2, PHP HTTP.
if you are using oauth2, goto libraries/oauth2/provider.php and uncomment the code line 182 shows
$ci = get_instance();
$ci->load->spark('curl/1.2.1');
$ci->curl
->create($url)
->post($params, array('failonerror' => false));
$response = $ci->curl->execute();
Here's how to do the Google oAuth properly:
$config = (object) array(
'CLIENT_ID' => 'AAAAA',
'CLIENT_SECRET' => 'BBBBB',
'REFRESH_TOKEN' => 'CCCCC',
);
$sJSON = file_get_contents('https://www.googleapis.com/oauth2/v4/token',FALSE,
stream_context_create(array('http'=>array(
'ignore_errors' => TRUE, // see errors in response instead of empty on error
'method' => 'POST',
'header' => array(
'Content-Type: application/x-www-form-urlencoded'
),
'content' => http_build_query(array(
'grant_type' => 'refresh_token',
'client_id' => $config->CLIENT_ID,
'client_secret' => $config->CLIENT_SECRET,
'refresh_token' => $config->REFRESH_TOKEN
))
)))
);
You can then use json_decode() to parse $sJSON into an object and then retrieve the access_token property.
For those who are wondering how to get the CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN, watch this video. It's not easy. In my case, I needed to do this for Google Adwords API. So, I had to get my Developer Token and Login Customer ID from https://ads.google.com/home/tools/manager-accounts. Then, I had to go to https://console.developers.google.com/apis/credentials with this guide to generate a Client ID and Client Secret. Then, I followed this guide to learn how to get my Refresh Token.