【发布时间】:2019-11-26 15:17:07
【问题描述】:
我想使用 google pay API 创建登机牌。我跟进了Get access to REST API steps,然后为了提出飞行类请求(基于此link),我在.net 中使用Google.Apis.Auth。发布请求后我得到未经授权。
这是我的代码:
var credential = GoogleCredential.FromFile(@"~\Certificate\AbomisWallet-4bca1ef0e957.json");
credential = credential.CreateScoped("https://www.googleapis.com/auth/wallet_object.issuer");
var serviceC = credential.UnderlyingCredential as ServiceAccountCredential;
JToken myJtoken;
using (StreamReader r = new StreamReader(@"~\Temp\myJson.json"))
{
string json = r.ReadToEnd();
myJtoken = JToken.FromObject(json);
}
var content = new StringContent(myJtoken.ToString(), Encoding.UTF8, "application/json");
serviceC.HttpClient.BaseAddress = new Uri("https://walletobjects.googleapis.com/walletobjects/v1/flightClass");
var result = serviceC.HttpClient.PostAsync(new Uri ("https://walletobjects.googleapis.com/walletobjects/v1/flightClass")
, content).Result;
这是我的错误:
{ "error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED" }}
【问题讨论】:
-
你能告诉我们错误在哪里,并给我们完整的错误信息吗?谢谢!
-
@CaseyCrookston 我编辑我的问题。
-
好的!嗯,错误很明显。
Request is missing required authentication credential。我不能花时间阅读他们所有的文档并弄清楚你为什么/如何没有正确地进行身份验证。但我敢肯定,以前也有人问过同样的问题。做一些谷歌工作。此外,还有一个用于使用 Google API 的 nuget 包,可以使这更容易:github.com/googleapis/google-api-dotnet-client -
请参阅库和示例以执行此操作以抽象出令牌工作。 github.com/angelbarranco/passes-rest-samples/tree/master/csharp
标签: c# .net google-api google-oauth google-pay