![](/img/trans.png)
[英]Passport.js: passport-facebook-token strategy, login through JS SDK and THEN authenticate passport?
[英]Add authorization header to Snapchat login callback using passport js strategy
我正在嘗試將使用 Snapchat 的登錄集成到我的應用程序中。 為此,我需要向我的應用程序的回調請求添加一個授權承載,以便我可以驗證客戶端。 根據Snapchat 指南,我需要使用 _qs 或 Axios 來創建請求:
// ******************** URL Builder Helper ***********************
var _qs = require("qs"); // Will need to 'npm install qs'
var getAuthCodeRedirectURL = function getAuthCodeRedirectURL(
clientId,
redirectUri,
scopeList,
state
) {
var SNAP_ACCOUNTS_LOGIN_URL =
"https://accounts.snapchat.com/accounts/oauth2/auth";
var scope = scopeList.join(" ");
var loginQS = {
client_id: clientId,
redirect_uri: redirectUri,
response_type: "code",
scope: scope,
state: state,
};
var stringifyLoginQS = _qs.stringify(loginQS);
return SNAP_ACCOUNTS_LOGIN_URL + "?" + stringifyLoginQS;
};
但是我不確定如何將鏈接包含在 passport.js 策略中。
你能澄清一下它是如何工作的嗎?
要通過授權 header,您必須在請求庫中設置標頭:
// Set headers
const headers = {
Authorization: "Basic " + authorizationHeaderBase64,
};
// Configure access token POST request
const options = {
url: SNAPCHAT_AUTH_ENDPOINT,
method: "POST",
headers: headers,
form: {...},
};
request(options)
但是,如果您使用 Passport 身份驗證,只需使用密鑰並像文檔中那樣設置配置就可以了:
passport.use(new SnapchatStrategy({
clientID: snapchat_APP_ID,
clientSecret: snapchat_APP_SECRET,
callbackURL: "http://localhost:3000/auth/snapchat/callback"
},
function(accessToken, refreshToken, profile, cb) {
User.findOrCreate({ snapchatId: profile.id }, function (err, user) {
return cb(err, user);
});
}
));
app.get('/auth/snapchat',
passport.authenticate('snapchat'));
app.get('/auth/snapchat/callback',
passport.authenticate('snapchat', { failureRedirect: '/login' }),
function(req, res) {
// Successful authorization, redirect home.
res.redirect('/');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.