问题描述
我遵循了在我的应用程序中使用谷歌应用程序引擎中的默认凭据。但是,在本地运行应用程序时,出现401错误(无效的凭据)
这是我使用的代码,包括教程的各个部分:
列表与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帐户
在您进行身份验证并授权后,您可以开始使用应用程序默认凭据,因为您已经正确设置了环境。
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)
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.
- First things first : install google cloud storage SDK
- 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...
- You need to authenticate with from within the SDK by running thegcloud auth activate-service-account [Service account email] --key-file [.p12 file] . When you run this you should get a message telling you that you have activated your service account
- You need to set environment variables from the SDK by settingGOOGLE_APPLICATION_CREDENTIALS to the JSON path of the secretCLOUDSDK_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中的默认凭据:无效的凭据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!