我在使用this关于使用端点v2(从昨天v1迁移)向我的webapp添加Firebase身份验证的教程。从JavaScript调用API前端:401未经授权使用angularfire
我以前只有Google帐户身份验证,但希望切换到Firebase以添加Facebook的。
当我从我的JavaScript Frontend(使用angularJS & angularfire构建)调用我的API时,我得到401未经授权的。
我的感觉,我缺少一个合乎逻辑的步骤:
我可以登录在客户端(弹出打开并显示我的Facebook名称)。
失踪步骤?
endpoints.get_current_user()不会获得用户。
我在哪里出错了?
这是我想,我想从后端配置文件以初始化页面的内容:
`/**
* Initialize profile page.
* Update the profile if the user's profile has been stored.
*/
$scope.init = function() {
var retrieveProfileCallback = function() {
$scope.profile = {};
$scope.loading = true;
gapi.client.myapi.getProfile().execute(function (resp) {
$scope.$apply(function() {
$scope.loading = false;
if (resp.error) {
// Failed to get a user profile.
} else {
// Succeeded to get the user profile.
$scope.profile.displayName = resp.result.displayName;
$scope.profile.someOtherProperty = resp.result.someOtherProperty;
$scope.initialProfile = resp.result;
}
});
}
);
};
if (!firebaseUser) {
//TODO
} else {
retrieveProfileCallback();
}
};`
这是ultimatively从getProfile()调用的方法的起点 - 终点:
def _getProfileFromUser(self):
"""Return user Profile from datastore, creating new one if non-existent."""
## Make sure user is authed
user = endpoints.get_current_user()
if not user:
raise endpoints.UnauthorizedException('Authorization required')
这里是我的API装饰(openapi.json已经部署):
# - - - - firebase - - - - - - - - - - - - - - - - - -
firebase_issuer = endpoints.Issuer(
issuer='https://securetoken.google.com/appname-123456',
jwks_uri='https://www.googleapis.com/service_accounts/v1/metadata/x509/[email protected]')
# - - - - Endpoints API - - - - - - - - - - - - - - - - - - -
@endpoints.api(name='myapi',
version='v1',
scopes=[EMAIL_SCOPE],
issuers={'firebase': firebase_issuer})
class MyApi(remote.Service):
我觉得我很大程度上误解了教程。这看起来太简单了,不起作用。 例如对于谷歌账号的授权,我初始化的oauth2 API,像这样在index.html的:
`<script>
function init() {
gapi.client.load('myapi', 'v1', null, '//' + window.location.host + '/_ah/api');
gapi.client.load('oauth2', 'v2', function() {
angular.bootstrap(document, ['conferenceApp']);
});
};
</script>`
我把说出来,因为我想我switiching到火力地堡。 像这样:
`<script>
/**
* Initializes the Google API JavaScript client. Bootstrap the angular module after loading the Google libraries
* so that Google JavaScript library ready in the angular modules.
*/
function init() {
gapi.client.load('myapi', 'v1', null, '//' + window.location.host + '/_ah/api', function() {
angular.bootstrap(document, ['myApp']);
});
};
</script>`