我在使用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>`