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



我正在尝试在Google Hire发布工作。先决条件是按照此处的规定完成的。我在 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;
}

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

我可以知道我哪里做错了???

提前谢谢。

代码

var keyFilePath = @"C:UsersLindaLDocuments.credentialsServiceAccount.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();

上面的代码应该使用 idexing API 为您请求令牌。

最新更新