【问题标题】:set Certificate file of google credentials, without using file设置谷歌凭证的证书文件,不使用文件
【发布时间】:2015-02-17 01:31:22
【问题描述】:

类: com.google.api.client.googleapis.auth.oauth2.GoogleCredential 创建实例时需要certifcate file as a parameter

我在谷歌应用引擎上使用 Jersey,如果我使用 guava Resources 打开我的文件(在资源中),一切都在本地正常工作,但是在部署到应用引擎时我得到URI is not Hierarchical error

所以我应该使用 getResourceAsStream 和 convert to temporary file ...

但谷歌应用引擎限制了 FileOutputStream 的使用(不允许使用)。

有没有什么方法可以在不使用 FileOutputStream 的情况下从输入流创建临时文件...或者是否有人熟悉使用 google OAuth api 的不同方式

【问题讨论】:

  • 无论您采用何种方式,都无法在 App Engine 上创建文件。您可能希望使用 Blobstore 来保存类似的内容,或者将文件保存在 GCS 本身上。由于 App Engine 可以(并且将)将您的应用程序从一个实例移动到另一个实例,因此让您的应用程序接触文件系统是不安全且非常复杂的

标签: java google-app-engine oauth jersey


【解决方案1】:

在已部署的应用引擎中执行此操作的正确方法是:

Collection<String> scopes = new ArrayList<>(1);
scopes.add("https://www.googleapis.com/auth/bigquery");
AppIdentityCredential credential = new AppIdentityCredential(scopes);
Bigquery bigquery = new Bigquery.Builder(new NetHttpTransport(), new GsonFactory(), credential).setApplicationName("myAppName").build();

这与本地开发/测试时推荐的方式不同 - 依赖于文件引用(因此您有不同的测试/生产代码):

String account = properties.get("oauthAccount");
String clientId = properties.get("oauthClientId");
String secret = properties.get("oauthSecret");
File privateKey = getCertficateFile();
GoogleCredential.Builder builder = new GoogleCredential.Builder();
builder.setTransport(new NetHttpTransport());
builder.setServiceAccountId(account);
builder.setJsonFactory(new GsonFactory());
builder.setClientSecrets(clientId, secret);            
builder.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/bigquery"));
builder.setServiceAccountPrivateKeyFromP12File(privateKey);
GoogleCredential googleBigQueryCredential = builder.build();
Bigquery bigquery = new Bigquery.Builder(new NetHttpTransport(), new GsonFactory(), googleBigQueryCredential).setApplicationName("myAppName")                                                                                                                     .build();

【讨论】:

    猜你喜欢
    • 2021-04-23
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2017-11-05
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多