使用 dotnet 访问受身份识别代理保护的 GCloud AppEngine URL 的正确方法是什么?
What is correct way to access an Identity Aware Proxy protected GCloud AppEngine URL with dotnet?
我有一个 GCloud 部署的应用程序引擎,它受 Google 的身份感知代理 (IAP) 保护,我想进行服务器到服务器通信以使用 dotnet 从应用程序中检索 URL (在本例中为 F#)。所有 google 示例都命中了 google api 而不是应用引擎页面,而且我还没有在 .Net 中找到任何示例。下面我的基本 F#(API 调用与你为 C# 所做的调用相同)设法设置服务凭证并检索令牌,但拉取应用程序的主页(应该不需要进一步的身份验证)returns 下面的错误。我认为它至少部分正确,因为之前提供 Bearer 令牌的尝试错误地登陆了 google 登录页面而不是应用程序。我已将服务帐户添加到授权用户列表中。 The closest google example is in Python 并使用两步过程,但 api 调用与 dotnet 库不同。感谢调试或修复的建议,
Unhandled Exception: System.AggregateException: One or more errors
occurred. (Response status code does not indicate success: 401
(Unauthorized).) ---> System.Net.Http.HttpRequestException: Response
status code does not indicate success: 401 (Unauthorized). at
System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at
System.Net.Http.HttpClient.d__27.MoveNext()
open Google.Apis.Auth
open Google.Apis.Requests
open Google.Apis.Http
open Google.Apis.Oauth2.v2
open Google.Apis.Auth.OAuth2
open System.IO
open System
open Google.Apis.Auth.OAuth2
open System
open System.Threading.Tasks
open Giraffe.Tasks
open System.Security.Cryptography.X509Certificates
open Google.Apis.Auth.OAuth2
open Google.Apis.Services
open System.Security.Cryptography
open System.Net.Http.Headers
let test() =
printfn "inial service account credentials from json"
use jsonStream = new StreamReader("c:/<path to json credentials>.json")
let credential = ServiceAccountCredential.FromServiceAccountData(jsonStream.BaseStream)
let t =
task {
printfn "Get access token"
// Is this the right auth_uri?
let! token = credential.GetAccessTokenForRequestAsync("https://www.googleapis.com/oauth2/v4/token")
let auth = AuthenticationHeaderValue("Bearer",token)
credential.HttpClient.DefaultRequestHeaders.Authorization<-auth
// This access to an Identity aware proxy protected appengine page access to an Identity aware proxy throws a 401
let! homePage = credential.HttpClient.GetStringAsync("https://<myapp>.appspot.com")
printfn "%s" homePage
}
t.Wait()
()
直到 5 分钟前,几乎不可能弄清楚,因为没有 .NET 示例或文档。但是,是的, 可以从 .NET 应用程序向受身份识别代理 (IAP) 保护的应用引擎站点发出 HTTP 请求。
我们刚刚重新发布
https://cloud.google.com/iap/docs/authentication-howto
所以它现在包含 C# 示例代码,以及一个 link 到 github.
上的完整示例
我有一个 GCloud 部署的应用程序引擎,它受 Google 的身份感知代理 (IAP) 保护,我想进行服务器到服务器通信以使用 dotnet 从应用程序中检索 URL (在本例中为 F#)。所有 google 示例都命中了 google api 而不是应用引擎页面,而且我还没有在 .Net 中找到任何示例。下面我的基本 F#(API 调用与你为 C# 所做的调用相同)设法设置服务凭证并检索令牌,但拉取应用程序的主页(应该不需要进一步的身份验证)returns 下面的错误。我认为它至少部分正确,因为之前提供 Bearer 令牌的尝试错误地登陆了 google 登录页面而不是应用程序。我已将服务帐户添加到授权用户列表中。 The closest google example is in Python 并使用两步过程,但 api 调用与 dotnet 库不同。感谢调试或修复的建议,
Unhandled Exception: System.AggregateException: One or more errors occurred. (Response status code does not indicate success: 401 (Unauthorized).) ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.d__27.MoveNext()
open Google.Apis.Auth
open Google.Apis.Requests
open Google.Apis.Http
open Google.Apis.Oauth2.v2
open Google.Apis.Auth.OAuth2
open System.IO
open System
open Google.Apis.Auth.OAuth2
open System
open System.Threading.Tasks
open Giraffe.Tasks
open System.Security.Cryptography.X509Certificates
open Google.Apis.Auth.OAuth2
open Google.Apis.Services
open System.Security.Cryptography
open System.Net.Http.Headers
let test() =
printfn "inial service account credentials from json"
use jsonStream = new StreamReader("c:/<path to json credentials>.json")
let credential = ServiceAccountCredential.FromServiceAccountData(jsonStream.BaseStream)
let t =
task {
printfn "Get access token"
// Is this the right auth_uri?
let! token = credential.GetAccessTokenForRequestAsync("https://www.googleapis.com/oauth2/v4/token")
let auth = AuthenticationHeaderValue("Bearer",token)
credential.HttpClient.DefaultRequestHeaders.Authorization<-auth
// This access to an Identity aware proxy protected appengine page access to an Identity aware proxy throws a 401
let! homePage = credential.HttpClient.GetStringAsync("https://<myapp>.appspot.com")
printfn "%s" homePage
}
t.Wait()
()
直到 5 分钟前,几乎不可能弄清楚,因为没有 .NET 示例或文档。但是,是的, 可以从 .NET 应用程序向受身份识别代理 (IAP) 保护的应用引擎站点发出 HTTP 请求。
我们刚刚重新发布 https://cloud.google.com/iap/docs/authentication-howto 所以它现在包含 C# 示例代码,以及一个 link 到 github.
上的完整示例