【发布时间】:2018-05-17 12:39:07
【问题描述】:
我正在尝试在 Google App Engine 上创建一个 API,它可以由具有身份验证的 .NET 客户端调用。
.NET 客户端使用验证用户身份
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "<client id>",
ClientSecret = "<client secret"
},
new[] { "email", "profile", "https://www.googleapis.com/auth/devstorage.read_write" }, "user", CancellationToken.None);
而且效果很好。
我有一个受保护的 servlet
<security-constraint>
<web-resource-collection>
<web-resource-name>servlet</web-resource-name>
<url-pattern>/servlet/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
所以只有经过身份验证的用户才能访问它,这就是我想要的。
我希望能够让当前用户使用
User currentUser = UserServiceFactory.getUserService().getCurrentUser();
但我无法让 .NET 客户端对 servlet 进行身份验证。
我试过了
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {credential.Token.AccessToken}");
httpClient.PutAsync("http://<my app>/servlet", content);
但是这失败了,因为 put 返回一个 302 重定向到一个以 https://www.google.com/accounts/ServiceLogin... 开头的 URL,.NET HTTP 客户端可能会遵循它,因为它最终以不允许的 405 方法结束。
我也试过
Google.Apis.Http.HttpClientFactory httpClientFactory = new HttpClientFactory();
Google.Apis.Http.CreateHttpClientArgs httpClientArgs = new CreateHttpClientArgs();
Google.Apis.Http.ConfigurableHttpClient httpClient = httpClientFactory.CreateHttpClient(new CreateHttpClientArgs());
credential.Initialize(httpClient);
(凭据是上面从 GoogleWebAuthorizationBroker.AuthorizeAsync 获得的 UserCredential)
但是,它做同样的事情 - 发出重定向到 google/accounts/ServiceLogin URl - 失败。
我正在尝试做的事情是否可能? 当然,servlet 必须有一种方法能够验证通过 API 而不是从 Web 以编程方式调用它的用户?
【问题讨论】:
-
我不是 100% 确定我理解你想要做什么。但是,您对用户进行身份验证的唯一方法是让您在他们的机器上向他们显示 Web 浏览器窗口。
-
我通过调用 GoogleWebAuthorizationBroker.AuthorizeAsync 向他们展示了 Web 浏览器。这将返回一个包含访问令牌的 UserCredential 对象。然后,我想以某种方式使用该令牌调用我自己制作的 AppEngine servlet,使用 PUT 或 POST,并让该 servlet 知道哪个用户正在调用它。 PUT 或 POST 来自 .NET 客户端,而不是来自网页。
-
类似于这个问题:stackoverflow.com/questions/14034043/…,但我没有使用“Google Apps 脚本”,而是使用 App Engine。如果 AppEngine 无法做到这一点,我可以使用什么替代平台来完成此任务?
-
appengine 运行 .net?你在使用.net 客户端库吗?
-
AppEngine 不运行 .net,不,appengine 运行 java。我正在尝试使用 .NET 客户端调用 AppEngine API
标签: google-app-engine google-oauth