简体   繁体   English

如何使用C#获取OAuth AccessToken(google)以将作业发布到Google Hire?

[英]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. 我正在尝试在Google招聘中发布工作。 Prerequsites are done as metioned here . 先决条件已按此处所述完成。 I got the service account credentials in a json file. 我在json文件中获得了服务帐户凭据。

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. 提前致谢。

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. 上面的代码应使用idexing api为您请求令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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