Getting 'The request had invalid authentication credentials' when using service account json key file in Go application

WBOY
Release: 2024-02-09 12:18:20
forward
1144 people have browsed it

在 Go 应用程序中使用服务帐户 json 密钥文件时获取“请求具有无效的身份验证凭据”

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.

Question content

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)
    }
Copy after login

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
}
Copy after login

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.

Workaround

You can rungcloud auth application-default login 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
Copy after login

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
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!