【发布时间】:2015-06-08 22:04:54
【问题描述】:
我想做的是将对我的服务端点的访问限制为仅部署在 Google App Engine 上的网络应用程序,因此,只能从 some-app-id.appspot.com 和映射到它的域 mymappeddomain 进行访问.com。我也不希望用户登录以使 API 工作。因此,基本上我希望 API 只能由托管在同一 Google App Engine 应用程序实例中的 JavaScript 代码访问,而无需用户登录以使用该服务。理想情况下,我的 API 在 API Explorer 中也不可见。
我对此进行了一些研究,找到了几篇文章(sample code,walk through),但没有成功应用它们。此外,似乎所有解决方案似乎仍然需要用户登录。我已经使用我的 Google Developer Console 创建了一个 Web 应用程序客户端 ID。
端点用 Go 编写并部署到 Google App Engine。类似这样:
package my
import ( ... "github.com/GoogleCloudPlatform/go-endpoints/endpoints" ... )
func (ms *MyService) ListData(c endpoints.Context, r *MyRequest) (*MyResponse, error) {
_, err := endpoints.CurrentBearerTokenUser(c,
[]string{ endpoints.EmailScope },
[]string{ "some-long-id-matching-what-is-used-in-javascript.apps.googleusercontent.com" })
if err != nil {
log.Printf("auth failed")
return nil, err
}
...
我的 JavaScript 看起来像:
var app = angular.module('myApp', ['ngRoute', 'angular-google-gapi']);
app.run(['$window', 'GAuth', 'GApi', function ($window, GAuth, GApi) {
var CLIENT = 'some-long-id-matching-what-is-used-in-go.apps.googleusercontent.com';
var BASE;
if($window.location.hostname === 'localhost') {
BASE = '//localhost:8080/_ah/api';
} else {
BASE = 'https://my-app-id.appspot.com/_ah/api';
}
GApi.load('myservice', 'v1', BASE);
GAuth.setClient(CLIENT);
GAuth.setScopes('https://www.googleapis.com/auth/userinfo.email');
GAuth.checkAuth().then(function () {
console.log('authenticated');
}, function () {
console.log('authentication issue');
});
}]);
当我从浏览器运行此应用程序时,我在 JavaScript 控制台中看到以下错误:
angular-google-gapi.min.js:7 myservice v1 api loaded
https://content.googleapis.com/oauth2/v2/userinfo Failed to load resource: the server responded with a status of 401 (OK)
app.js:24 authentication issue
我很想就如何使这项工作提出建议。提前谢谢你。
【问题讨论】:
标签: google-app-engine google-oauth google-cloud-endpoints