Default Credentials in Google App Engine: Invalid

2019-09-03 08:40发布

I followed the tutorial on https://developers.google.com/identity/protocols/application-default-credentials to use the default credentials in my application on the google app engine. However, when running the app locally, I get a 401 error (invalid Credentials)

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 OK { "code" : 401, "errors" : [ { "domain" : "global", "location" : "Authorization", "locationType" : "header", "message" : "Invalid Credentials", "reason" : "authError" } ], "message" : "Invalid Credentials" }

This is the code I use, included the parts of tutorial:

List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
        try{
            // Authorize the request.
            GoogleCredential credential = GoogleCredential.getApplicationDefault(); 
            if (credential.createScopedRequired()) {
                credential = credential.createScoped(scopes);   
            }
            // This object is used to make YouTube Data API requests.
            youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
                    .setApplicationName("youtube-cmdline-captions-sample").build();
        } catch (IOException e) {
            e.printStackTrace();
        }

I have the Google Cloud SDK installed, I used the shell to give permission to get access to my google account

1条回答
Fickle 薄情
2楼-- · 2019-09-03 09:20

There are several steps that you need to follow in order to use the application default credentials... You can follow the steps below, or have a look at this link.

  1. First things first : install google cloud storage SDK
  2. Make sure you can run commands from the SDK. If you do not have python installed you need to install python 2.7 or above, and also pyopenssl...
  3. You need to authenticate with from within the SDK by running the gcloud auth activate-service-account [Service account email] --key-fil e [.p12 file] . When you run this you should get a message telling you that you have activated your service account
  4. You need to set environment variables from the SDK by setting GOOGLE_APPLICATION_CREDENTIALS to the JSON path of the secret CLOUDSDK_PYTHON_SITEPACKAGES to 1, and also setting the project

Commands to configure system variables...

set GOOGLE_APPLICATION_CREDENTIALS "secret.json path"
set CLOUDSDK_PYTHON_SITEPACKAGES 1
gcloud config set project "your project name"

After you authenticate, and authorize yourself, you can then start using the applications default credentials, given that you have setup your environment properly.

查看更多
登录 后发表回答