【问题标题】:How to get access token in google oauth in C#(ASP.NET)?如何在 C#(ASP.NET) 的 google oauth 中获取访问令牌?
【发布时间】:2021-05-14 05:05:55
【问题描述】:

我正在使用 C#。我成功获得了授权码,但我不知道如何获得访问令牌。我更喜欢Google website。但是,我不知道如何在 C#(ASP.net) 中应用它。

【问题讨论】:

标签: c# asp.net google-api oauth-2.0


【解决方案1】:

我已成功使用 dotnetopenauth 库(在 SO 中使用它)来访问 Google 的服务。我建议不要使用自己的令牌管理器,而是使用现有的令牌管理器并将其扩展到您的需求。

【讨论】:

    【解决方案2】:

    Google Drive SDK 包含一个完整的 ASP.NET MVC 示例,您可以将其用作参考:

    https://developers.google.com/drive/examples/dotnet

    【讨论】:

      【解决方案3】:

      像这样尝试。

      String client_id = "<OAUTH_client_id >";
      String client_secret = "<OAUTH_client_secret>";
          
      var BaseAddress = "https://adddress.com";
      var httpClient = new HttpClient() { BaseAddress = new Uri(BaseAddress) };
            
      var creds = $"client_id={client_id}&client_secret{client_secret}&grant_type=client_credentials";
      httpClient.DefaultRequestHeaders.Accept.Clear();
      httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
      var content = new StringContent(creds, Encoding.UTF8, "application/x-www-form-urlencoded");
      var response = httpClient.PostAsync("/oauth2/token", content).Result;
      var jsonContent = response.Content.ReadAsStringAsync().Result;
          
      var tokenObj = JsonConvert.DeserializeObject<BearerViewModel>(jsonContent);
      var token = tokenObj.access_token;
      

      【讨论】:

        猜你喜欢
        • 2012-08-03
        • 2011-08-21
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        • 1970-01-01
        • 2012-02-12
        相关资源
        最近更新 更多