Google App Engine GoogleCredential getApplicationDefault

时间:2017-12-08 06:39:41

标签: google-app-engine oauth

我正在尝试使用GoogleCredential getApplicationDefault()来访问我已使用应用引擎信息中心创建的应用引擎默认服务帐户。代码如下所示。

    try{
        GoogleCredential credential = GoogleCredential.getApplicationDefault();
        if (credential.createScopedRequired()){
            ArrayList<String> scope = new ArrayList<String>();
            scope.add("https://www.googleapis.com/auth/devstorage.read_write");
            GoogleCredential scopedCredential = credential.createScoped(scope);
            scopedCredential.refreshToken();
            return scopedCredential.getAccessToken();
        }
    }
    catch(Exception ex){
        entityException = new Entity(Constants.ENTITY_EXCEPTION);
        entityException.setProperty("Exception",ex.getMessage());
        datastore.put(entityException);
    }
    return "";

调用scopedCredential.refreshToken()时,邮件accounts.google.com

会发生异常

当我调用scopedCredential.getServiceAccountId()和scopedCredential.getServiceAccountPrivateKeyId()时,我得到了默认应用引擎服务帐户的正确值。

任何人都可以帮助我。

各位,

有人能帮助我吗?我尝试添加一个新的服务帐户,创建一个p12文件,而不是json等。似乎没有任何工作。我不知道我哪里出错了。请帮忙。

1 个答案:

答案 0 :(得分:0)

嘿,我自己都明白了。我将运行时指定为java8,这导致了所有问题。猜测这些只适用于java7运行时。感谢。