如何在 C# 中获取 OAuth AccessToken (google) 以将工作发布到 Google Hire?

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

我想 post 在 Google 招聘一份工作。先决条件如 here 所述完成。我在 json 文件中获得了服务帐户凭据。

我尝试使用以下方式获取访问令牌:

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;
        }

但问题是执行函数后,它只是挂断/停止响应。我无法在 "token " 中获得任何值。

请问我哪里做错了???

提前致谢。

代码

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();

以上代码应使用索引 api 为您请求令牌。