googleapis / google-auth-library-python

Google Auth Python Library
https://googleapis.dev/python/google-auth/latest/
Apache License 2.0
771 stars 304 forks source link

IAM_PERMISSION_DENIED #1550

Closed dlxj closed 1 month ago

dlxj commented 1 month ago
# pip install --upgrade google-cloud-iam  google-auth google-auth[pyopenssl]  arrow

import os, datetime, arrow
from google.oauth2 import service_account
import google.auth.transport.requests

if __name__ == "__main__":
    credentials = service_account.Credentials.from_service_account_file(
        filename='/root/xxx.json',  # os.environ["GOOGLE_APPLICATION_CREDENTIALS"],
        scopes=["https://www.googleapis.com/auth/cloud-platform"],
    )
    request = google.auth.transport.requests.Request()

    if not credentials.token:
        credentials.refresh(request)
        expiry = credentials.expiry
        clears_time = str(arrow.get(expiry).shift(hours=+8))

    print( credentials.token )

    import requests

    url = "https://us-east5-aiplatform.googleapis.com/v1/projects/xxx/locations/us-east5/publishers/anthropic/models/claude-3-haiku@20240307:rawPredict"
    headers = {
        "Authorization": f"Bearer {credentials.token}",
        "Content-Type": "application/json"
    }
    data = {"anthropic_version": "vertex-2023-10-16", "messages": [{"role": "user", "content": "Send me a recipe for banana bread."}], "max_tokens": 100, "stream": True}

    response = requests.post(url, headers=headers, json=data, stream=True)

    if response.status_code == 200:
        for line in response.iter_lines():
            if line:
                print(line.decode('utf-8'))
    else:
        print(f"Error: {response.status_code}")

    # IAM_PERMISSION_DENIED
dlxj commented 1 month ago

service account set permissions: Consumer Procurement Entitlement Manager Vertex AI User