[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentai: client authentication fails #4494

Closed
taalhach opened this issue Jul 24, 2021 · 7 comments
Closed

documentai: client authentication fails #4494

taalhach opened this issue Jul 24, 2021 · 7 comments
Labels
api: documentai Issues related to the Document AI API. needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.

Comments

@taalhach
Copy link

Client

DocumentAI

Go Environment

$ go version
o version go1.15.6 linux/amd64

Code

package main
import (	
documentai "cloud.google.com/go/documentai/apiv1"
"google.golang.org/api/option"
documentaipb "google.golang.org/genproto/googleapis/cloud/documentai/v1"
)
func main() {


		req := &documentaipb.ProcessRequest{
			Source: &documentaipb.ProcessRequest_RawDocument{
				RawDocument: &documentaipb.RawDocument{
					Content:  fileBytes,
					MimeType: "application/pdf",
				},
			},
			Name: "projects/{{PROJECT_ID}}/locations/us/processors/{{PROCESSOR_ID}}",
		}

		opt := option.WithCredentialsFile("/path/to/file/cred.json")
		ctx := context.Background()
		c, err := documentai.NewDocumentProcessorClient(ctx, opt)
		if err != nil {
			log.Debugf("failed to create client, err: %v", err)
			os.Exit(0)
		}
		defer c.Close()

		resp, err := c.ProcessDocument(ctx, req)
		if err != nil {
			log.Debugf("failed to process document, err: %v", err)
			os.Exit(0)
		}

}

Expected behavior

Client should process document

Actual behavior

rpc error: code = Unauthenticated desc = Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

Screenshots

image
Additional context

I have created service account and using credential from that account and while creating role i have created Basic > Owner role to give full access for now but i tried with document AI user role also. Still no luck on the other hand I have tried pubsub client with same credentials and it works perfectly fine. Is there something that i am doing wrong or this is bug with documentai client ??

@taalhach taalhach added the triage me I really want to be triaged. label Jul 24, 2021
@taalhach
Copy link
Author

@codyoss can you please check this or mention someone who can confirm this bug or [recommend any solution?
thanks :)

@codyoss codyoss changed the title DocumentAI client authentication fails documentai: client authentication fails Jul 26, 2021
@codyoss codyoss added needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. labels Jul 26, 2021
@codyoss
Copy link
Member
codyoss commented Jul 26, 2021

Hey @taalhach sorry you are having problems with authentication. My best guess is that you are pointing to a service account key that is no long active on your project. I tried to reproduce this with the sample code given and it work for me when I pointed to a valid service account with the correct permissions.

@taalhach
Copy link
Author

@codyoss as you can see from screenshot it says it is an active key and i have generated more keys and tried with those but no luck.

opt := option.WithCredentialsFile("path/to/key-fa7a66f98203.json")

c, err := documentai.NewDocumentProcessorClient(ctx, opt)

image

does it have anything to do with gcp cli as i haven't installed it because client libraries can take care of auth by themselves ?

@taalhach
Copy link
Author

@codyoss or is there something wrong with my service account setup as i have created using instructions here

@product-auto-label product-auto-label bot added the api: documentai Issues related to the Document AI API. label Jul 27, 2021
@taalhach
Copy link
Author
taalhach commented Jul 27, 2021

finally after searching and spending a lot of time i found that was an issue of scopes when we pass credentials via file we need to add an extra option.ClientOption which specifies the scopes for oauth2 access token. as we use service account so need to pass IAM API scope. unfortunately documentation does not even have any clue about that, i hope it can be of some help for someone. hit upvote here at slackoverflow which really helped me.
final piece of code:

		req := &documentaipb.ProcessRequest{
			Source: &documentaipb.ProcessRequest_RawDocument{
				RawDocument: &documentaipb.RawDocument{
					Content:  bytes,
					MimeType: "application/pdf",
				},
			},
			Name: "projects/{PROJECT_ID}/locations/us/processors/{PROCESSOR_ID}",
		}

		opt := option.WithCredentialsFile("path/to/credentials/file.json")
		ctx := context.Background()
                scopes := documentai.DefaultAuthScopes()
		c, err := documentai.NewDocumentProcessorClient(ctx, opt, option.WithScopes(scopes...))
		if err != nil {
			log.Debugf("failed to create client, err: %v", err)
			os.Exit(0)
		}
		defer c.Close()

		resp, err := c.ProcessDocument(ctx, req)
		if err != nil {
			log.Debugf("failed to process document, err: %v", err)
			os.Exit(0)
		}

@codyoss
Copy link
Member
codyoss commented Jul 27, 2021

@taalhach Thanks for reporting back. I am still not able to reproduce this issue, but I am glad you got your code working. Default scopes should be added to the client for you unless you override them. The scope you are manually passing here looks to be the scope add be default. 1 2

@taalhach
Copy link
Author

@codyoss thanks for simplifying this piece of code really appreciate that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: documentai Issues related to the Document AI API. needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants