使用GCE API插入新实例

时间:2014-03-17 15:22:09

标签: java api google-compute-engine

我编写了一个使用java API在GCE中创建实例的代码 但是这段代码不起作用我得到以下错误

403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}

我的代码发布如果它有任何错误或是否需要任何库导入请帮助我 我导入的库是

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeScopes;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.InstanceList;
import com.google.api.services.compute.model.NetworkInterface;
import com.google.api.services.compute.model.Operation;
import com.google.api.services.compute.model.Zone;
import com.google.api.services.compute.model.ZoneList;



  private static void createInstance(String projectId, JsonFactory jsonFactory,Compute compute) throws IOException {
        Instance instance = new Instance();
        instance.setFactory(jsonFactory);

        // Select a machine type.
        String machine = "https://www.googleapis.com/compute/v1/projects/hindproj/global/machineTypes/n1-standard-1";
        instance.setMachineType(machine);

        // Get a name from the user.
        String name = "v1";
        instance.setName(name);
        // Use the default network.  Could select here if needed.
        List<NetworkInterface> networkInterfaces = new ArrayList<NetworkInterface>();
        NetworkInterface iface = new NetworkInterface();
        iface.setFactory(jsonFactory);
        iface.setName("eth0");
        iface.setNetwork("https://www.googleapis.com/compute/v1/projects/hindproj/global/networks/default");//( COMPUTE_API + "/projects/" + projectId + "/networks/default");
        networkInterfaces.add(iface);
        instance.setNetworkInterfaces(networkInterfaces);

        // Select a zone.
        String zone = "https://www.googleapis.com/compute/v1/projects/hindproj/zones/us-central1-b";
        instance.setZone(zone);
        Compute.Instances.Insert ins = compute.instances().insert(projectId,zoneName, instance);

        // Finally, let's run it.
        Operation op = ins.execute();
        System.out.println(op.toPrettyString());
        System.out.println(instance.toPrettyString());
      }

1 个答案:

答案 0 :(得分:0)