Google App Engine中的默认凭据:无效的凭据错误 [英] Default Credentials in Google App Engine: Invalid Credentials error

查看:383
本文介绍了Google App Engine中的默认凭据:无效的凭据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了 https://developers.google.com/identity上的教程/ protocols / application-default-credentials 在我的应用程序中使用谷歌应用程序引擎中的默认凭据。但是,在本地运行应用程序时,出现401错误(无效的凭据)


com.google.api.client.googleapis.json .GoogleJsonResponseException:401
OK {code:401,errors:[{
domain:global,
location:Authorization,
locationType:header,
message:证书无效,
reason:authError}],message:证书无效}

这是我使用的代码,包括教程的各个部分:

 列表与LT;字符串> scopes = Lists.newArrayList(https://www.googleapis.com/auth/youtube.force-ssl); 
尝试{
//授权请求。
GoogleCredential凭证= GoogleCredential.getApplicationDefault();
if(credential.createScopedRequired()){
credential = credential.createScoped(scopes);
}
//此对象用于制作YouTube数据API请求。
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT,Auth.JSON_FACTORY,凭证)
.setApplicationName(youtube-cmdline-captions-sample)。build();
} catch(IOException e){
e.printStackTrace();
}

我已安装Google Cloud SDK,我使用shell授予权限访问我的Google帐户

解决方案

您需要遵循几个步骤才能使用应用程序默认凭据。 。您可以按照以下步骤进行操作,或查看此链接

  • 确保您可以运行来自SDK的命令。如果您没有安装python,您需要安装python 2.7或更高版本,以及pyopenssl ...

  • 您需要通过运行
    gcloud auth activate-service-account [服务帐户电子邮件] --key-fil $ b $是[.p12文件]。当您运行此应用程序时,您应该会收到一条消息,告诉您已激活服务帐户。

  • 您需要通过将
    GOOGLE_APPLICATION_CREDENTIALS设置为JSON路径来设置SDK中的环境变量将秘密的
    CLOUDSDK_PYTHON_SITEPACKAGES设置为1,并设置项目

  • 配置系统变量的命令...

     
    set GOOGLE_APPLICATION_CREDENTIALSsecret.json path
    set CLOUDSDK_PYTHON_SITEPACKAGES 1
    gcloud config set projectyour project name



    在您进行身份验证并授权后,您可以开始使用应用程序默认凭据,因为您已经正确设置了环境。


    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

    解决方案

    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.

    这篇关于Google App Engine中的默认凭据:无效的凭据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆