![](/img/trans.png)
[英]How to pass data from one route to another nodejs with expressjs
[英]Passing uber authentication from one route to another with nodejs
因此,我在nodejs中使用uber API,試圖使用uber服務,例如價格和時間估算,以及通過我的應用程序發出的乘車請求。
我可以授權用戶並返回一個回調URL,但是我不知道如何將創建的訪問令牌或product_id傳遞給特定的/ requests或/ estimates路由。
這是我的服務器文件:
// Authenticate uber login with scopes
app.get('/v1.2/login', function(request, response) {
var url = uber.getAuthorizeUrl(['profile', 'request', 'places',
'all_trips', 'ride_widgets']);
response.redirect(url);
log(url);
// User can manually go to authorization @
https://login.uber.com/oauth/v2/authorize?
client_id=LJGpana69PX47lPLFP5PpIdySYT5CT-G&response_type=code
});
// Redirect script to authorize uber profile with oAuth 2.0
app.get('/v1.2/callback', function(request, response) {
uber.authorizationAsync( {
authorization_code: request.query.code
})
.spread(function(access_token, refresh_token, authorizedScopes,
tokenExpiration) {
// store the user id and associated access_token, refresh_token,
scopes and token expiration date
log('New access_token retrieved: ' + access_token);
log('... token allows access to scopes: ' + authorizedScopes);
log('... token is valid until: ' + tokenExpiration);
log('... after token expiration, re-authorize using
refresh_token: ' + refresh_token);
var query = url.parse(request.url, true).query;
return uber.products.getAllForLocationAsync(query.lat,
query.lng);
})
.then(function(res) {
log(res);
// redirect the user back to your actual app
response.redirect('../Client/index.html');
})
.error(function(err) {
console.error(err);
});
});
app.post('/v1.2/requests', function(request, response) {
// extract the query from the request URL
//var query = url.parse(request.url, true).query;
uber.requests.createAsync({
"fare_id": j,
"start_latitude": request.query.lat,
"start_longitude": request.query.lng,
"end_latitude": request.query.goalat,
"end_longitude": request.query.goalng
})
.then(function(res) {
log(res);
uber.requests.getCurrentAsync()
.then(function(res) {
log(res);
res.send('got it');
})
.error(function(err) {
console.error(err);
});
})
.error(function(err) {
console.error(err);
});
});
就像我之前說過的,我只是不知道傳遞訪問令牌並將product_id傳遞到請求路由或其他路由。 該文檔實際上沒有給出任何示例。
我在這里先向您的幫助表示感謝
對於第一個問題,通過執行uber.authorizationAsync
,您已經獲得了access_token。 它是存儲在包裝對象中的屬性,您可以使用uber.access_token
檢索它。
至於第二個問題,您已經通過uber.getAllForLocationAsync
獲得了可用的產品ID。 但是,您不會為請求調用緩存它們。 我建議按照README中關於node-uber的說明來拆分產品ID調用。 這樣,您就可以在請求的方法中調用product方法,並獲取JSON響應,其中包括發出請求所需的產品ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.