Androidpublisher The current user has insufficient permissions to perform the requested operation.

Date : 23-07-2024

I wanted to share some steps on how to set up Google Play In-App Purchases using Google_Service_AndroidPublisher. Here’s a brief walkthrough:

First, you'll need to use the following code snippet:



use Google_Service_AndroidPublisher;

$client = new Google_Client();
$client->setAuthConfig(resource_path('path_to_your_key.json')); // Use the credentials you used for your purchase setup
$client->setScopes('https://www.googleapis.com/auth/androidpublisher');

$packageName = 'com.bakadur.app';
$purchaseToken = $request->google_token; // Make sure you handle validations here
$productId = $request->premium_type;   // And here as well

$service = new Google_Service_AndroidPublisher($client);
$response = $service->purchases_products->get($packageName, $productId, $purchaseToken);



In case you encounter the following error:



{
  "error": {
    "code": 401,
    "message": "The current user has insufficient permissions to perform the requested operation.",
    "errors": [
      {
        "message": "The current user has insufficient permissions to perform the requested operation.",
        "domain": "androidpublisher",
        "reason": "permissionDenied"
      }
    ]
  }
}




Here’s what you need to do to resolve it:

  1. Enable the API:
  2. Go to the Google Play Android Developer API page in Google Cloud Console and click on "Enable".
  3. Create a Service Account:
  4. Navigate to IAM & Admin in the Google Cloud Console and create a service account.
  5. Fill in the service account details (name the project as you like).
  6. Grant the service account access to the project with the following roles:
  7. Pub/Sub Admin (enables Platform Server Notifications)
  8. Monitoring Viewer (allows monitoring of the notification queue)
  9. Create and Download the Public Key:
  10. In the 'Service Accounts' section, select the three dots in the Actions dropdown menu, then Manage Keys.
  11. Add a new key, select JSON, then create and download the JSON key.
  12. Grant Access in Google Play Console:
  13. Go to Users and Permissions in the Google Play Console and invite the service account you created.
  14. Grant the following permissions:
  15. View app information and download bulk reports (read-only)
  16. View financial data, orders, and cancellation survey responses
  17. Manage orders and subscriptions

Once you have completed these steps, try running the code again. The error should disappear.

Good luck with your setup!