saggaf.arsyad
saggaf.arsyad

Reputation: 831

Failed to authenticate Service Account credential in Google Cloud Prediction API Library for NodeJS

I was using googleapis library and successfully authenticate my Service Account credentials using JWT. Then I found out google-cloud library, which has specific modules for Prediction API, but failed to authenticate the same Service Account credentials.

I don't use Google Computing Engine or App Engine and working in local developement

I follow code in documentation:

var key = require('./MY_SERVICE_ACCOUNT_CREDENTIALS.json')
var prediction = require('@google-cloud/prediction')({
  projectId: 'MY_PROJECT_ID',
  credentials: key
})

prediction.getModels((err, models) => {
  if (err) {
    console.log(err)
    return
  }

  console.log('Models:', models)
})

and it returns:

{ ApiError: Access to project denied: user requires read permissions.                                              
    at [here goes stack trace]                                                                                   
  code: 401,                                                                                                       
  errors:                                                                                                          
   [ { domain: 'global',                                                                                           
       reason: 'authError',                                                                                        
       message: 'Access to project denied: user requires read permissions.',                                       
       locationType: 'header',                                                                                     
       location: 'Authorization' } ],                                                                              
  response: undefined,                                                                                             
  message: 'Access to project denied: user requires read permissions.' } 

I don't know where it goes wrong and I'm not sure if I would need to authenticate with googleapis and integrate it with google-cloud or etc. It's also lack of samples

Upvotes: 1

Views: 613

Answers (1)

Steven Lu
Steven Lu

Reputation: 2180

So I know this is a couple of months old but the error is pretty self explanatory but I also ran into this issue myself.

Essentially your service key isn't given the proper permissions in order to read (or write) information from your project. Look for your service account under IAM & Admin in the Google Cloud Console and add Project → Editor and/or Project → Viewer to the service account you've created. Make sure you press "Save" once you've added those permissions and wait about 20 seconds for those permissions to reflect.

Upvotes: 1

Related Questions