私はMEANスタックを使用して構築したSPAでfb認証を実装しています。 Facebookのトークンパスポート戦略を使用してfb認証を実装している間は、APIのエンドポイントを確保する際の問題に直面しています。そのため、$ httpサービスで認証されたユーザーオブジェクトとアクセストークンの両方を渡す必要があるため、ユーザーオブジェクトのプロパティとして、またヘッダープロパティとしてaccess_tokenを渡そうとしましたが、まだ401(Unauthorized error)です。以下は私のコードスニペットです。
パスポートのドキュメントには、「Authorization:Bearer base64_access_token_string」と書かれています。トークンをbase64形式でエンコードする必要がありますか? Plsヘルプ。
サーバコード
app.get('/api/getbikes*',
passport.authenticate('facebook-token',{session: false}),
function(req,res){
if(req.user){
console.log('In getbikes api');
// console.log('req.query :',req.query);
var msg="";
ubBike
.find({cust:req.query._id})
.populate('cust','email')
.exec(function(err,bikes){
res.send(bikes);
if(err) throw err;
});
}
else
{
res.send(401);
}
});
角度コード
サービス
this.getbikes = function(user){
var deferred = $q.defer();
$http({
method:"GET",
url:"http://localhost:3000/api/getbikes",
params: user,
headers:{
Authorization:auth.getAccesstoken()
}
}).then(function successCallback(srresponse){
deferred.resolve(srresponse.data);
},
function failureCallback(srresponse){
$log.error("get bikes http call failed ",srresponse.data);
deferred.reject(srresponse.data);
});//$http
return deferred.promise;
};//getbikes
コントローラ
$scope.fblogin= function(){
auth.fblogin().then(
function(response){
$scope.isAuth = auth.isAuth;
$scope.usr =auth.getResponseobj();
$scope.usr.access_token=auth.getAccesstoken();
$scope.profpic=auth.profpic;
bike.getbikes($scope.usr).then(function(response){
if (response.length ==0)
{
$location.path('/addbike');//redirect to addbike screen
}
else{
$location.path('/appoint');//else redirect to view appointment screen
}
},function(reason){
$scope.msg1 = reason;
});//getbikes
},function(reason){
$log.log("fblogin() - failure :Need to login to the application :"+reason);
})
};//fblogin
httpヘッダーにaccesstokenを送信する場合は、この形式である必要があります。「認証:ベアラーのアクセス権の付与」。あなたのヘッダー認証はそのようになりますか?また、あなたのコントローラーでは、 'user.us 'ではなく' $ scope.usr'を送ります。それもチェックしてください。 – Karthik
また、あなたのサーバーコードをデバッグし、あなたの 'req'オブジェクトに何が含まれているのか調べてみてください。 – Karthik
@ Karthik、 "Bearer accesstokenstring"の形式はどういう意味ですか?それは当初私が立ち往生していたものです。これはトークンの先頭に「ベアラー」を付けることを意味しますか? pls clarify –