Fabrizio Ferrari
Fabrizio Ferrari

Reputation: 989

Google API for Android Publisher: API key not valid

I am trying to access the androidpublisher API via a standard Google API procedure in PHP as shown below:

<?
require __DIR__ . '/vendor/autoload.php';

$credentialsFilePath = '/path/to/key_file.json'; //replace this with your actual path and file name
$client = new \Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/androidpublisher');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();

$bearer = $token['access_token'];

$url = 'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/products/{productId}/tokens/{token}?key={My_API_key}';


$headers = array(
    'Content-Type:application/json',
    'Authorization: Bearer '.$bearer
);
            
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

print "result below:\n";
print_r($result);

?>

But despite I have created correctly the key JSON file loaded above, and entered the same corresponding API key at the end of the $url, I keep getting this result:

{
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "errors": [
      {
        "message": "API key not valid. Please pass a valid API key.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "androidpublisher.googleapis.com"
        }
      }
    ]
  }
}

I have set {My_API_key} as defined on the API KEYS panel as shown in the shot below (I have removed the key in the shot, of course):

enter image description here

Is there any other key I need to use instead?

Upvotes: 1

Views: 439

Answers (1)

Fabrizio Ferrari
Fabrizio Ferrari

Reputation: 989

Ok, after spending a few more hours, I understood that I don't need the "?key=" definition after the token. Second, these pages helped me to restore the correct response by just updating one of my in-app item definitions on Goolgle Play Console:

"The current user has insufficient permissions to perform the requested operation.” - Google Play Developer API

https://github.com/googleapis/google-api-nodejs-client/issues/1382

Upvotes: 1

Related Questions