'How to get OAuth AccessToken (google) for posting a job to Google Hire in C#?

I'm trying to post a job at Google Hire. Prerequsites are done as metioned here . I got the service account credentials in a json file.

I tried to get Access Token using :

var jsonFilePath = HttpContext.Current.Server.MapPath("~/GoogleKey/client_secret.json");


var token = GetAccessTokenFromJSONKey(path,"https://www.googleapis.com/auth/indexing"); 

public static async Task<string> GetAccessTokenFromJSONKeyAsync(string jsonKeyFilePath, params string[] scopes)
        {
            using (var stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read))
            {
                return await GoogleCredential
                    .FromStream(stream) // Loads key file  
                    .CreateScoped(scopes) // Gathers scopes requested  
                    .UnderlyingCredential // Gets the credentials  
                    .GetAccessTokenForRequestAsync(); // Gets the Access Token  
            }

        }


 public static string GetAccessTokenFromJSONKey(string jsonKeyFilePath, params string[] scopes)
        {
            return GetAccessTokenFromJSONKeyAsync(jsonKeyFilePath, scopes).Result;
        }

but the problem is after executing the function, it just hangs up/ stops responding. I'm unable to get any value in "token " .

May I know as to where am I doing wrong ???

Thanks in advance.



Solution 1:[1]

Code

var keyFilePath = @"C:\Users\LindaL\Documents\.credentials\ServiceAccount.json";
var clientEmail = "1046123799103-6v9cj8jbub068jgmss54m9gkuk4q2qu8@developer.gserviceaccount.com";
var scopes = new[] { "https://www.googleapis.com/auth/indexing" };

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
    {
    credential = GoogleCredential.FromStream(stream)
    .CreateScoped(scopes);
    }

var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();

The above code should request a token for you using the idexing api.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1