3

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Raj Kumar
  • 65
  • 1
  • 8

1 Answers1

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks a lot. This work like a charm. I ve a another doubt that after posting jobs to google hire using indexing api i m getting response as 200 but how to get know that whether it is published or not on their site?How long it will take to reflect? – Raj Kumar Jul 04 '18 at 15:20
  • no idea its a new API I haven't had time to test it myself – Linda Lawton - DaImTo Jul 04 '18 at 16:49
  • have tested these APIs. Actually my urls getting indexed but still not published on Google Jobs. Can you please help whom to contact or what to do to get it published? – Raj Kumar Jul 22 '18 at 17:04
  • might get some help here https://github.com/google/search-samples/issues – Linda Lawton - DaImTo Jul 22 '18 at 17:08