我正在通過OAuth連接到第三方API,Intuit Quickbooks以獲取用戶的憑據。我已經實現了回調,因爲他們需要並可以成功獲取憑據。在回調中,我調用流星方法函數,以傳入的憑據作爲參數更新當前登錄用戶的配置文件。但是,Meteor無法找到用戶。從第三方API回調未在MeteorJS中獲得當前用戶
我試過使用Meteor.Async和BindEnvironment,但我得到同樣的問題。當API返回回調時,是否有可能知道哪個用戶向第三方API發起了請求?
var stuff;
Router.route('/requestToken', { name: 'requestoken', where: 'server' })
.get(function (req,res) {
var QuickBooks = require('node-quickbooks');
var request = require('request');
var qs = require('querystring');
var consumerKey = 'xxxxx',
consumerSecret = 'xxxxx'
var postBody = {
url: QuickBooks.REQUEST_TOKEN_URL,
oauth: {
callback: 'http://localhost:3000/callback/',
consumer_key: consumerKey,
consumer_secret: consumerSecret
}
}
console.log('abt to request')
request.post(postBody, function (e, r, data) {
var requestToken = qs.parse(data)
stuff = requestToken.oauth_token_secret;
console.log(requestToken)
res.writeHead(301, {
'Location': QuickBooks.APP_CENTER_URL + requestToken.oauth_token
});
res.end();
})
})
.post(function() {
// POST
})
.put(function() {
// PUT
})
Router.route('/callback', { name:'callback' , where: 'server' })
.get(function() {
var req = this.request;
var res = this.response;
var QuickBooks = require('node-quickbooks');
var request = require('request');
var qs = require('querystring');
var consumerKey = 'xxxxx',
consumerSecret = 'xxxxx'
var postBody = {
url: QuickBooks.ACCESS_TOKEN_URL,
oauth: {
consumer_key: consumerKey,
consumer_secret: consumerSecret,
token: req.query.oauth_token,
token_secret: stuff,
verifier: req.query.oauth_verifier,
realmId: req.query.realmId
}
}
request.post(postBody, Meteor.bindEnvironment(function (e, r, data) {
var accessToken = qs.parse(data)
qbo = new QuickBooks(consumerKey,
consumerSecret,
accessToken.oauth_token,
accessToken.oauth_token_secret,
postBody.oauth.realmId,
true, // use the Sandbox
true); // turn debugging on
// test out account access
qbo.findAccounts(function(_, accounts) {
accounts.QueryResponse.Account.forEach(function(account) {
console.log(account.Name)
})
});
// save the access token somewhere on behalf of the logged in user
var userCred = {
token:accessToken.oauth_token,
secret:accessToken.oauth_token_secret,
real:postBody.oauth.realmId
}
console.log("about to call addQB details");
console.log(userCred);
//this method simply adds the userCred to the current logged in user's profile.
//Whenever the method below is called from here, it never finds the user. However when called from the client it works well.
//Is it possibe to know logged user from a 3rd party API callback?/How can I go about this?
Meteor.call("addUserQb",userCred);
}));
res.end('<!DOCTYPE html><html lang="en"><head></head><body><script>window.opener.location.reload(); window.close();</script></body></html>')
})
.post(function() {
// POST
})
.put(function() {
// PUT
})
而且方法如下。 (用假證件的obj作爲參數,從客戶端調用時的方法效果很好,但在路由器從回調稱爲以上時不會得到用戶)。
addUserQb:function(obj){
Meteor.users.update(Meteor.userId(), {
$set:{
'profile.token': obj.token,
'profile.secret': obj.secret,
'profile.realmId' : obj.real
}
});
}
這現在更有意義。還沒有嘗試過你的解決方案,但會對它起作用。謝謝! – pascal