尝试获取正在运行的实例列表时,找不到资源'projects/<my project>'错误

jKi*_*imo 6 google-app-engine google-api-java-client google-compute-engine

我的目标是通过首先检索活动实例列表来测试google的orchestrator和计算引擎api.包含servlet文件的orchestrator项目存储在jar中.

我正在尝试测试java google计算引擎客户端api.我有一个调用orchestrator servlet的cron作业.cron的目标是后端.我从中尝试获取实例列表:

...
AppIdentityCredential credential = getCredential(computeScope);

String appName = ConfigProperties.getInstance().getGceConfigProperties().get("projectId");

try {
    httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    final Compute compute = new Compute.Builder(
        httpTransport, JSON_FACTORY, credential).setApplicationName(appName)
        .build();
    logger.info("================== Listing Compute Engine Instances ==================");

    Compute.Instances.List instances = compute.instances().list(projectId, zone);
    InstanceList list = instances.execute();

    if (list.getItems() == null) {
        logger.info("No instances found. Sign in to the Google APIs Console and create "
            + "an instance at: code.google.com/apis/console");
    } else {
        for (Instance instance : list.getItems()) {
            logger.info(instance.toPrettyString());
        }
    }
...
Run Code Online (Sandbox Code Playgroud)

我得到的错误响应是(我从响应中省略了我的项目名称,我确认我在我的代码中使用了正确的项目ID):

com.google.cloud.solutions.sampleapps.orchestration.orchestrator.server.GceClientApiUtils      
getInstances: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 OK
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "message" : "The resource 'projects/<project-name-here>' was not found",
    "reason" : "notFound"
  } ],
  "message" : "The resource 'projects/<project-name_here>' was not found"
}
Run Code Online (Sandbox Code Playgroud)

我还尝试通过检索访问令牌并进行RESTful调用来获取实例列表并收到完全相同的响应.我通过将它与使用api explorer成功查询实例进行比较,确认构建的Url是正确的.

编辑:我在另一篇文章的帮助下确定了该问题的解决方案:我终于能够在后期计算引擎API调用中找到解决方案失败了http 404

我需要将我的应用引擎服务帐户添加为具有编辑功能的团队成员,默认情况下它没有.一旦我这样做,代码按预期工作.我必须通过cloud.google.com/console执行此操作,就像通过appengine.google.com完成一样,将向服务帐户提供待处理状态,并且无权访问.

sup*_*rdo 5

对我来说,我必须确保我有授权。在终端试试这个gcloud auth login


小智 2

您可以访问开发者控制台https://console.developers.google.com吗?用户帐户 @appspot.gserviceaccount.com 似乎无法访问计算引擎。就我而言,我看到@developer.gserviceaccount.com。

如果您没有,请访问https://developers.google.com/console/help/new/#generateoauth2创建一个新的客户端 ID