【问题标题】:Get unauthorized in adding boarding pass to google pay未经授权将登机牌添加到 Google Pay
【发布时间】: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


【解决方案1】:

这太疯狂了,但您需要手动获取和设置令牌。

像这样改变你的代码

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

            var token = await serviceC.GetAccessTokenForRequestAsync();
            serviceC.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

            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;

抱歉我的英语不好。

【讨论】:

猜你喜欢
  • 2016-11-25
  • 2019-09-09
  • 2019-07-07
  • 2023-03-31
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
相关资源
最近更新 更多