Thursday, August 3, 2017

Re: [google-cloud-sql-discuss] Google_Service_SQLAdmin notauthorized error - howto fix?

Hooray!!!  Error disappears after I  add "Cloud SQL Admin" to original "Compute Instance Admin (V1)" (so now role shows as multiple).  However response gives empty array (I have one stopped instance-1 that I expect to see).  Maybe I have to specify region.

On Thursday, August 3, 2017 at 3:31:38 PM UTC-7, David Newgas wrote:
Have you made sure to replace IDofMyProject with the actual ID of your project? Have you got service account credentials at the path you have in your code? Is that file readable by user executing your PHP code (probably your webserver)? Does the service account you are using have one of the owner, editor, viewer, cloudsql.admin, cloudsql.editor or cloudsql.viewer roles on the project?

On Thu, Aug 3, 2017 at 3:12 PM, Aseire Heard <blog...@gmail.com> wrote:
Getting a notauthorized error when I try to use the sqlinstances.php example from https://developers.google.com/api-client-library/php/auth/service-accounts#sqlinstancesphp to get a list of my instances.  I added scopes (and authorized using API manager).  What am I doing wrong here please?

php sqlinstances.php

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notAuthorized",
    "message": "The client is not authorized to make this request."
   }
  ],
  "code": 403,
  "message": "The client is not authorized to make this request."
 }
}

and here is the code:

<?php

// https://developers.google.com/api-client-library/php/auth/service-accounts#sqlinstancesphp

// Autoload Composer.
require_once __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$client->addScope('https://www.googleapis.com/auth/compute');
$client->addScope('https://www.googleapis.com/auth/compute.readonly');
$client->addScope('https://www.googleapis.com/auth/sqlservice.admin');

$sqladmin = new Google_Service_SQLAdmin($client);

// Project ID of the project for which to list Cloud SQL instances.
$project = 'IDofMyProject';  // TODO: Update placeholder value.
$optParams = [];
$response = $sqladmin->instances->listInstances($project, $optParams)->getItems();

echo json_encode($response) . "\n";
?>

--
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/a94ba5aa-fc3f-4dec-b4e2-cdb7baa0eb43%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/5daad22a-7363-4ef9-935b-0aa2137a01eb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment