Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google authentication from PHP

I know there are a lot of similar questions here in SO but I tried these solutions for hours but they didn´t work for me. I always get a { "error" : "unauthorized_client" }". I want to programmatically refresh my accesstoken to use the Youtube API. I already have gained a refreshtoken.

This is what I´ve come up with:

$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_secret' => '<mysecret>',
'grant_type' => 'refresh_token',
'refresh_token' => '<my_refresh_token>',
'client_id' => '<my_client_id>.apps.googleusercontent.com',
'redirect_url'=>'<my_redirect_uri>'
));
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
echo var_dump(curl_exec($ch));

Whats wrong with it?

like image 631
Rainer Sauerstoff Avatar asked Nov 18 '25 03:11

Rainer Sauerstoff


1 Answers

The URL and query params you've indicated look right to me. Seems like this error would come up if the client_id you provide to generate new tokens is different than the client_id provided to obtain the refresh_token.

One thing that might be happening is that if you have generated an access_token and refresh_token using Google's OAuth playground, and then trying to use that refresh_token to generate new tokens -- this will not work. The Google OAuth playground is using different client_ids to make that request, and this will definitely result in the "unauthorized_client" error you've documented.

Temboo has a very concise and easy-to-use OAuth library for Google. You can check it out here: https://www.temboo.com/library/Library/Google/OAuth/.

(Full disclosure: I work at Temboo)

like image 134
ajennings Avatar answered Nov 19 '25 16:11

ajennings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!