简体   繁体   中英

Google Cloud Rest API C#

I am having troubles with Google Cloud API. I was successfully logged in and created a new key with the API, but the problem is to connect with the new key because the JSON file that was created in the first time by Google (in the UI) is different from the JSON that came from the API. I cannot log-in with the new key, Google is throwing an error.

My code:

    private static void ServiceAccounts(string credentialsJson)
    {
        LoginAsServiceAccountWithJson(credentialsJson);

        IList<ServiceAccountKey> serviceAccountKeys = ListKeysOfServiceAccount(userEmail);

        ServiceAccountKey newKey = CreateKeyOfServiceAccount(userEmail);

        //DeleteKeyOfServiceAccount(newKey);

        LoginWithServiceAccountKey(newKey, credentialsJson);
    }

    public static void LoginAsServiceAccountWithJson(string credentialsJson)
    {
        //Create credentials from the JSON file that we receive from GCP.
        GoogleCredential credential = GoogleCredential.FromJson(credentialsJson)
        .CreateScoped(IamService.Scope.CloudPlatform);

        // Create the Cloud IAM service object
        s_service = new IamService(new IamService.Initializer
        {
            HttpClientInitializer = credential
        });

        ListRolesResponse response = s_service.Roles.List().Execute();
    }

    public static ServiceAccountKey CreateKeyOfServiceAccount(string userEmail)
    {
        var newKeyDetails = s_service.Projects.ServiceAccounts.Keys.Create(
            new CreateServiceAccountKeyRequest(), "projects/-/serviceAccounts/" + userEmail);

        ServiceAccountKey key = newKeyDetails.Execute();
        return key;
    }

    public static IList<ServiceAccountKey> ListKeysOfServiceAccount(string userEmail)
    {

        IList<ServiceAccountKey> keys = s_service.Projects.ServiceAccounts.Keys
            .List($"projects/-/serviceAccounts/{userEmail}").Execute().Keys;

        return keys;
    }

The code is working, except the function "LoginWithServiceAccountKey" ,I cannot use the Key that I just received from Google in order to login. The JSON is different, even the key is a lot bigger in the API (i saw that in the key from the UI the size is 1735 chars and from the API is 3000+). PS - I know that I should wait 60 seconds before trying to connect, it's not the problem.

It took me some time to figure out how the APIs fit together. Here's a version of your ServiceAccounts() function that calls the Google Translate API with the new key:

   private static void ServiceAccounts(string credentialsJson, string userEmail)
   {
        LoginAsServiceAccountWithJson(credentialsJson);

        IList<ServiceAccountKey> serviceAccountKeys = ListKeysOfServiceAccount(userEmail);

        ServiceAccountKey newKey = CreateKeyOfServiceAccount(userEmail);

        // Call the translate API with the new key.
        byte[] jsonKey = Convert.FromBase64String(newKey.PrivateKeyData);
        Stream jsonKeyStream = new MemoryStream(jsonKey);
        ServiceAccountCredential credential = ServiceAccountCredential
            .FromServiceAccountData(jsonKeyStream);
        GoogleCredential googleCredential = GoogleCredential
            .FromServiceAccountCredential(credential);
        var client = TranslationClient.Create(googleCredential);
        var response = client.TranslateText("Hello World", "ru");
        Console.WriteLine(response.TranslatedText);           

        //DeleteKeyOfServiceAccount(newKey);

        // LoginWithServiceAccountKey(newKey, credentialsJson);
    }

Full code is here: https://github.com/SurferJeffAtGoogle/scratch/blob/master/StackOverflow53251345/Program.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM