Skip to content

Trying Google Apis (Server-to-server) but gives 401 error, even after assigning permissions to the service account. #3584

@shubyaa

Description

@shubyaa

Hi, can anyone drop the set of permissions for the service account, I tried the sample test code to call the API

import { GoogleAuth } from "google-auth-library";
import fetch from "node-fetch";

// Path to your service account JSON key file
const serviceAccountPath = "./firebase_service_account_key.json";

// Initialize the GoogleAuth client with the service account key and required scope
const authClient = new GoogleAuth({
  keyFile: serviceAccountPath,
  scopes: ["https://www.googleapis.com/auth/androidpublisher"],
});

const getAccessToken = async () => {
  const client = await authClient.getClient();
  const { token } = await client.getAccessToken();
  if (!token) throw new Error("Failed to retrieve access token");
  return token;
};

const callPlayDeveloperAPI = async (packageName, subscriptionId, token) => {
  try {
    const accessToken = await getAccessToken();

    const url = `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${packageName}/purchases/subscriptions/${subscriptionId}/tokens/${token}`;
    console.log("Request URL:", url);

    const response = await fetch(url, {
      method: "GET",
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });

    if (!response.ok) {
      const errorData = await response.json();
      console.error("Error Response:", JSON.stringify(errorData));
      throw new Error(
        `Request failed with status ${response.status}: ${errorData.error.message}`
      );
    }

    const data = await response.json();
    console.log("Subscription Data:", JSON.stringify(data));
  } catch (error) {
    console.error("Failed to call Play Developer API:", error);
  }
};

// Replace with actual values
const packageName = "com.myexample.app";
const subscriptionId = "xyz_tag";
const token = "my_token";

// Call the function to make the API request
callPlayDeveloperAPI(packageName, subscriptionId, token).catch(console.error);

This code still returns me 401 error, I have also done inviting service account in my play console. I feel like for the scope android publisher, I might me missing some permissions. The permissions associated with the current service account are these :

image

If anyone know what I might be missing, it will be a great help.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions