2016-12-24 25 views
0

我正在通過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 
     } 
    }); 
} 

回答

1

考慮一下是怎麼回事:服務器本身並不沒有一個userId。從客戶端調用的方法是通過DDP協議調用的,DDP協議是由客戶端創建並與userId關聯的持久連接,因此這些方法在其上下文中確實獲得了userId。

來自Router建立的路徑的http請求不與任何DDP會話相關聯,也不能有userId。 bindEnvironment()在這裏不會幫助你,它只能在回調函數中運行,函數運行在與最初通過DDP調用的方法相關聯的nodejs光纖中。

您有一個回調來自不同服務器的http請求。這是通過不同的路由,並且與將原始請求發送給Oauth提供者的服務器方法沒有任何關聯。

解決方案:您需要將userId信息添加到對Quickbooks的請求(可能位於回調URL中),以便Quickbooks Oauth服務器進行回調時,可以從請求中提取userId。

+0

這現在更有意義。還沒有嘗試過你的解決方案,但會對它起作用。謝謝! – pascal

相關問題