Monday, May 8, 2017

[google-cloud-sql-discuss] Re: How to get rid of error 401 on Cloud SQL API?

I just noticed that you mentioned you also get the same error in the OAuth2 playground, so this would be an access issue and not a code issue. It was also a bit confusing as you said the OAuth2 token was good because you got it from the playground, but it actually doesn't work in the playground :)

Can you detail how you're configuring the authorization options in the OAuth2 playground?

On Monday, May 8, 2017 at 5:05:09 PM UTC-4, Adam (Cloud Platform Support) wrote:
How are you using a service account if you're copying the access token from the OAuth2 Playground? To better handle authentication and simplify the process, it's recommend to use the Google API PHP Client library to interact with Google APIs.

On Monday, May 8, 2017 at 9:22:45 AM UTC-4, Maxime Arbez wrote:
Hi, I have the following code in order to add an new authorized address into Google Cloud SQL :

$access_token = "ya29.GltEBBtlPfCr1F-20oQq7GU-ZZWW9J7R9ftQc5II5oSnr7IKZGk2iMDN9CcRDG_njDzKctfD6g6N5HxOYk1wjRoSJSMyDevEqdlIxWFjId5ZOLnqfHnprWmtWK6Q";
$headers
= array('Authorization: Bearer ' . $access_token, 'Content-Type: application/json');

$send_method
= "PATCH";
$payload
= '{"settings" : {"ipConfiguration" : {"authorizedNetworks" : [{ "value": "220.20.0.1" }]}}}';


$settings
= array(
 CURLOPT_CUSTOMREQUEST
=> $send_method,
 CURLOPT_RETURNTRANSFER
=> true,
 CURLOPT_HTTPHEADER
=> $headers,
 CURLOPT_SSL_VERIFYPEER
=> 0,
 CURLOPT_POSTFIELDS
=> $payload
 
);
 
 
 $curl
= curl_init($url);

 curl_setopt_array
($curl, ($settings));


 $result
= curl_exec($curl);

 curl_close
($curl);


I know that my payload is OK because it works on https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/patch .
I also know that the token is good because I got it from the oAuth playground.
I use a service account and I set myself as the owner of it.

Nevertheless, I get the following error (I also get it into the oAuth playground) :
{   "error": {    "errors": [     {      "domain": "global",      "reason": "required",      "message": "Login Required",      "locationType": "header",      "location": "Authorization"     }    ],    "code": 401,    "message": "Login Required"   }  }


What can I do?
Thanks!

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/e0093342-01d9-4d00-a04e-6a5391a1e65f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment