Sunday, May 7, 2017

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

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/89644533-6626-4db5-bf60-7aef21dd3264%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment