繁体   English   中英

使用Node.js将uber身份验证从一条路由传递到另一条路由

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM