googleapis / google-cloud-go

Google Cloud Client Libraries for Go.
https://cloud.google.com/go/docs/reference
Apache License 2.0
3.77k stars 1.29k forks source link

storage: authentication to storage module using access token fails #8579

Closed pkk-code closed 8 months ago

pkk-code commented 1 year ago

Client

storage

Environment

Any

Go Environment

G0 1.21

Code Access token is generated from this code and passed to different module to authenticate.

Module 1: credentials, err := google.FindDefaultCredentials(ctx, scopes...) if err == nil { token, err = credentials.TokenSource.Token() if err != nil { log.Print(err) } }

Module 2: token.AccessToken from module1 is used to initialize the storage client .

projectId := "projectid"
accessToken := "token"
ctx := context.Background()
// Create a Google Cloud Storage client using the access token
storageClient, err := storage.NewClient(ctx, option.WithTokenSource(
    oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
))
if err != nil {
    log.Fatalf("Failed to create Storage service: %v", err)
}

it := storageClient.Buckets(ctx, projectId)
for {
    battrs, err := it.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("Bucket: %v\n", battrs.Name)
}

Expected behavior

Authenticated client that lists the buckets in the project

Actual behavior

googleapi: Error 401: Invalid Credentials, authError

Additional context

The identical concept functions correctly when using the Java SDK but encounters issues with the Go SDK, potentially due to configuration omissions within the code i listed above or in Go SDK. Could you please check this?

String accessTokenString = "token"; var accessToken = AccessToken.newBuilder().setTokenValue(accessTokenString).build(); var cred = OAuth2CredentialsWithRefresh.create(accessToken); Storage storageAccessToken = StorageOptions.newBuilder().setCredentials(cred).setProjectId("projectid").build().getService(); var list = storageAccessToken.list("bucket"); list.getValues().forEach(val -> { System.out.println(val.asBlobInfo().getName()); });

BrennaEpp commented 10 months ago

Hi @pkk-code, sorry for the delay. What environment are you on and do you know what kind of credentials are being used?

BrennaEpp commented 8 months ago

Closing this due to lack of response. Please open another issue if you are still having authentication difficulties.