0

我在 app -engine 上使用 RemoteAPI 进行谷歌身份验证(使用已弃用的 ClientLogin )。我想将其更改为 Oauth2.0 。我用谷歌搜索了很多,但没有找到太多解释。任何形式的帮助将不胜感激。

public abstract class RemoteApiClient {

    protected void doOperationRemotely() throws IOException {
        TestProperties testProperties = TestProperties.inst();

        System.out.println("--- Starting remote operation ---");
        System.out.println("Going to connect to:"
                + testProperties.PROJECT_REMOTEAPI_APP_DOMAIN + ":"
                + testProperties.PROJECT_REMOTEAPI_APP_PORT);

        RemoteApiOptions options = new RemoteApiOptions().server(
                testProperties.PROJECT_REMOTEAPI_APP_DOMAIN,
                testProperties.PROJECT_REMOTEAPI_APP_PORT).credentials(
                testProperties.TEST_ADMIN_ACCOUNT,
                testProperties.TEST_ADMIN_PASSWORD);

        RemoteApiInstaller installer = new RemoteApiInstaller();
        installer.install(options);
        try {
            doOperation();
        } finally {
            installer.uninstall();
        }

        System.out.println("--- Remote operation completed ---");
    }

}
4

1 回答 1

0

对于 OAuth2 身份验证,您在 gcloud 中有两个主要解决方案。

安装 gcloud ( https://cloud.google.com/sdk/ ),然后初始化它 ( https://cloud.google.com/sdk/docs/initializing )。之后,您应该运行“gcloud auth login”,它将提示您在 gmail 上连接帐户的身份验证窗口。

您的另一个解决方案是通过 googles 控制台创建一个新的服务帐户,为其提供一个 json 密钥并将其作为凭证传递给函数参数,或在 GOOGLE_APPLICATION_CREDENTIALS 环境变量中设置它或使用 gcloud auth activatve-service-account --key-file “ PATH_TO_JSON”。

创建服务帐户的教程:https ://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview

gcloud 默认身份验证优于服务帐户(如下所述)的主要优点是每个谷歌云产品都会在一个固定位置检查 application_credentials.json(well_known_file) 文件。另一个优点是它的令牌可以在一个多小时的会话期间刷新。

于 2016-05-18T06:35:15.627 回答