How to get rid of error 401 on Cloud SQL API?

166 views
Skip to first unread message

Maxime Arbez

unread,
May 8, 2017, 9:22:45 AM5/8/17
to Google Cloud SQL discuss
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!

Adam (Cloud Platform Support)

unread,
May 8, 2017, 5:05:09 PM5/8/17
to Google Cloud SQL discuss
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.

Adam (Cloud Platform Support)

unread,
May 8, 2017, 5:49:34 PM5/8/17
to Google Cloud SQL discuss
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?
Reply all
Reply to author
Forward
0 new messages