node.js - node-oauth Yahoo API oAuth2 issue -
i'm building app node.js , express.js. i'm using node-oauth module connect yahoo can make requests api. keep getting error below
{ statuscode: 401, data: '{"error":{"@lang":"en-us", "@uri":"http://yahoo.com", "description":"not authorized - either yt cookies or valid oauth token must passed authorization","detail":"not authorized - either yt cookies or valid oauth token must passed authorization"}}' } after trying while figure out problem, i'm asking community check out code , see doing wrong. code included below.
"use strict"; // declare libraries var express = require('express'); var router = express.router(); var oauth = require('oauth'); // set yahoo key , secret var yahookey = '*****************************************************'; var yahoosecret = '*********************************'; var oauth2 = new oauth.oauth2( yahookey, yahoosecret, 'https://api.login.yahoo.com/', 'oauth2/request_auth', 'oauth2/get_token', null ); router.get('/', function(req, res, next) { var access_token = oauth2.getoauthaccesstoken( '', {'grant_type':'authorization_code', 'redirect_uri':'http://www.domain.com'}, function (e, access_token, refresh_token, results) { // console.log(e); // done(); }); // console.log(oauth); oauth2.get( 'https://social.yahooapis.com/v1/user/circuitjump/profile?format=json', access_token, function (error, data, response){ if (error) { console.error(error); } // data = json.parse(data); // console.log(json.stringify(data, 0, 2)); // console.log(response); }); res.render('index', { title: 'express' }); }); // export route module.exports = router; any appreciated. brain fried ...
you seem missing steps. direct first guide:
https://developer.yahoo.com/oauth2/guide/flows_authcode/
first, starting path @ '/', need redirect (302) user authorization page (step 2 of yahoo's guide). oauth lib has helper generate correct url:
var location = oauth2.getauthorizeurl({ client_id: yahookey, redirect_uri: 'https://yourservice.com/oauth2/yahoo/callback', response_type: 'code' }); res.redirect(location); what did there redirected user's browser yahoo's authorization page, user gets dialog asking if want allow service xyz access stuff on user's behalf. upon clicking "allow", yahoo redirect browser callback url (step 3 of yahoo's guide), providing authorization code in query params. in example have hooked @ /oauth2/yahoo/callback
you can set (step 4 of yahoo's guide):
router.get('/oauth2/yahoo/callback', function(req, res) { // aha have authorization code! var code = req.query.code; oauth2.getoauthaccesstoken( code, { 'grant_type': 'authorization_code', 'redirect_uri': 'oob' }, function(e, access_token, refresh_token, results) { console.log('now have token', access_token, 'that can use call yahoo apis!'); res.end(); }); }); i hope of makes sense. i'll leave exercise figure out refresh token (step 5). if make far, part should easy :)
edit
it looks yahoo requires send key , secret in authorization basic header. can generate header , tell oauth2 module include so:
var encoded = new buffer(yahookey+":"+yahoosecret).tostring('base64') var authheader = "basic " + encoded; var oauth2 = new oauth.oauth2( yahookey, yahoosecret, 'https://api.login.yahoo.com/', 'oauth2/request_auth', 'oauth2/get_token', { authorization: "basic " + authheader} );
Comments
Post a Comment