Pithikos
Pithikos

Reputation: 20300

Google Client gives 400: RESOURCE_PROJECT_INVALID

I seem to be getting 400 when using Google's official client. Just googling RESOURCE_PROJECT_INVALID doesn't seem to be giving much help.

I create the credentials.json by simply creating a service in Google Cloud IAM and creating a key. I've checked and the project ID inside the json is correct.

from google.cloud import artifactregistry_v1
from google.oauth2 import service_account


credentials = service_account.Credentials.from_service_account_file('credentials.json')
client = artifactregistry_v1.ArtifactRegistryClient(credentials=credentials)
client.list_files()
InvalidArgument: 400 Invalid resource field value in the request. [reason: "RESOURCE_PROJECT_INVALID"
domain: "googleapis.com"
metadata {
  key: "service"
  value: "artifactregistry.googleapis.com"
}
metadata {
  key: "method"
  value: "google.devtools.artifactregistry.v1.ArtifactRegistry.ListFiles"
}
]

credentials.json (as generated from Google)

{
  "type": "service_account",
  "project_id": "myproject",
  "private_key_id": "cccccccccccccccccc",
  "private_key": "-----BEGIN PRIVATE KEY-----\naaaaaaaabbbbbbbb=\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "1000000000000000",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/myservice%40myproject.iam.gserviceaccount.com"
}

Any idea on what this means/how to fix it?

Upvotes: 2

Views: 8189

Answers (1)

DazWilkin
DazWilkin

Reputation: 40251

The documentation for list_files isn't ideal.

You will need to populate parent and probably best done as part of ListFilesRequest.

Try:

from google.cloud import artifactregistry_v1

# Using Application Default Credentails is easiest
# export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
client = artifactregistry_v1.ArtifactRegistryClient()

# Best obtained from the environment
project = "..."
location = "..."
repository = "..."

parent = f"projects/{project}/locations/{location}/repositories/{repository}"

request = artifactregistry_v1.ListFilesRequest(
    parent=parent,
)

page_result = client.list_files(
    request=request,
)

for response in page_result:
    print(response)

Although the documentation above describes this, another way to determine the correct call is to use APIs Explorer to lookup Artifact Registry API and find projects.locations.repositories.files.list method:

GET https://artifactregistry.googleapis.com/v1/{parent=projects/*/locations/*/repositories/*}/files

Here you can see that the method requires a parent property and its definition. The page also documents the response object.

Upvotes: 5

Related Questions