Search code examples
c#.netgoogle-cloud-platformgoogle-apiandroid-management-api

Service account is not authorized to manage project


I recently joined a team, and am adding Android Management Api to the already existing project. I added the management API, created service accounts with permissions, and am writing the .NET project to test it out.

I made a service account with Android Management User and Owner permissions. However, when I try to use the .NET library to make an enterprise, I get

The service androidmanagement has thrown an exception. HttpStatusCode is Forbidden. Caller is not authorized to manage project.

If it helps: The API key I'm using is allowed to call any API, and the application name is a temporary one that does NOT match the project name. As for the service account with private key, I am using a FileStream to read a .json file downloaded when the service account was created.

This is my code, based on the sample app https://developers.google.com/android/management/sample-app

The error gets thrown on the createRequst.Execute()

string CreateEnterprise()
{
    SignupUrlsResource.CreateRequest signupUrlRequest = managementService.SignupUrls.Create();
    signupUrlRequest.ProjectId = cloud_project_id;
    signupUrlRequest.CallbackUrl = "https://www.yahoo.com";
    var signupUrl = signupUrlRequest.Execute();

    string enterpriseToken = signupUrl.Url;
    Console.WriteLine("Signup: " + enterpriseToken);

    EnterprisesResource.CreateRequest createRequest = managementService.Enterprises.Create(new Enterprise());
    createRequest.ProjectId = "Test Project";
    createRequest.SignupUrlName = signupUrl.Name;
    createRequest.EnterpriseToken = enterpriseToken;
    var enterprise = createRequest.Execute();

    return enterprise.Name;
}

Solution

  • Turns out the createRequest.ProjectId must match the project name that has the Android Management API, aka the project I'm working with.