这个
GoogleCredential reference
向提供示例
还可以使用服务帐户流模拟您拥有的域中的用户。这与上面的服务帐户流非常相似,但您需要另外调用
GoogleCredential.Builder.setServiceAccountUser(String)
public static GoogleCredential createCredentialForServiceAccountImpersonateUser(
HttpTransport transport,
JsonFactory jsonFactory,
String serviceAccountId,
Collection<String> serviceAccountScopes,
File p12File,
String serviceAccountUser) throws GeneralSecurityException, IOException {
return new GoogleCredential.Builder().setTransport(transport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountId)
.setServiceAccountScopes(serviceAccountScopes)
.setServiceAccountPrivateKeyFromP12File(p12File)
.setServiceAccountUser(serviceAccountUser)
.build();
}
关于如何从App Engine Standard访问文件,有多个选项。下面介绍了两个选项:
选项1:
p12File
(凭据文件)是
App Engine Standard resource file
. 检查
this answer
要查看如何访问此类文件,请按代码进行。我强烈推荐这种方法,因为
这些文件仅在只读基础上对您的代码可用,不用于流量服务。
File f = new File("path/below/my/project/directory/credentials.json");
以及
appengine-web.xml
应定义资源文件:
<resource-files>
<include path="path/below/my/project/directory/credentials.json"/>
</resource-files>
选项2:凭据文件位于云存储中。这
document
解释如何从App Engine Standard访问云存储中的文件。但我认为这太麻烦了。我也提供了这个选项,因为我已经提到了它。