php Xiaobian Banana When using a service account JSON key file in a Go application, you may encounter the "Request has invalid authentication credentials" issue. This problem is usually caused by incorrect format or content of the key file. To resolve this issue, check that the key file is in the correct format and that the key file contains the correct authentication information. Also try regenerating the key file and making sure it is properly configured into the application. Through these methods, you can solve the "request has invalid authentication credentials" problem and use the service account JSON key file normally.
I am developing a go application on gcp project and am using google cloud logging service. I'm having trouble running the app because it says my authentication credentials are invalid when I use the service account json key.
This is the code snippet with the error:
c, cerr := load(".env") if cerr != nil { log.fatalf("could not load config: %s", cerr) return } // initializes logger which writes to stdout ctx := context.background() opt := option.withcredentialsfile(c.googleapplicationcredentials); loggerclient, clienterr := logging.newclient(ctx, "poc-projects-01", opt) if clienterr != nil { log.fatal(clienterr) }
This is the definition of load()
function:
func Load(file string) (*Config, error) { viper.SetConfigFile(file) viper.AddConfigPath(".") viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { return nil, err } c := &Config{ GoogleApplicationCredentials: viper.GetString("GOOGLE_APPLICATION_CREDENTIALS"), } return c, nil }
I have a .env
file with the following content:
google_application_credentials=json/path-to-json.json
I don't know why it says the token has expired even though this is the only service account json key I have on gcp and my local machine.
You can rungcloud auth application-default login
a> and make sure you have it set to the correct project.
Check googleapplicationscredentials
If a valid json key is set and the environment variable is set correctly, please run the following command to check
echo $google_application_credentials
If the command does not return the correct path to the json key, you can set the environment variable using the following command:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json/key.json
After verifying that the json key is valid and the environment variables are set correctly, you should be able to run your application. Alternatively, you can try deleting the .env file and then using the service account json key and it should regenerate the token and make it valid.
Additional troubleshootingDocumentation a> is for reference.
The above is the detailed content of Getting 'The request had invalid authentication credentials' when using service account json key file in Go application. For more information, please follow other related articles on the PHP Chinese website!