Skip to content

Instantly share code, notes, and snippets.

@idej
Created March 24, 2015 19:52
Show Gist options
  • Save idej/bafc3f10d843e4bbad2d to your computer and use it in GitHub Desktop.
Save idej/bafc3f10d843e4bbad2d to your computer and use it in GitHub Desktop.
getting yahoo oauth 2.0 tokens in your ionic application
//should install cordova InAppBrowser plugin
//http://docs.phonegap.com/en/edge/cordova_inappbrowser_inappbrowser.md.html
//yahoo configs
//YAHOO.clientId, clientSecret - your yahoo application values - https://developer.yahoo.com/
//YAHOO.redirectUri - your redirect uri (should be with domain that set in yahoo app configs)
var YAHOO = {
clientId: 'xxxxxxx',
clientSecret: 'xxxxxxx',
redirectUrl: 'your redirect url',
requestAuthUrl: 'https://api.login.yahoo.com/oauth2/request_auth',
requestTokenUrl: 'https://api.login.yahoo.com/oauth2/get_token'
}
var access_code = null,
yahooCreadentials = btoa(YAHOO.clientId + ':' + YAHOO.clientSecret),
yahooLoginUrl = YAHOO.requestAuthUrl +
'?client_id=' + YAHOO.clientId +
'&redirect_uri=' + YAHOO.redirectUrl +
'&response_type=code';
//open url in in app browser and
//add event listener to yahoo request on redirect url
var ref = window.open(yahooLoginUrl, '_blank', 'location=no');
ref.addEventListener('loadstart', function(event) {
if(event.url.indexOf(YAHOO.redirectUrl) == 0) {
access_code = (event.url).split("code=")[1];
//post on yahoo to get access and refresh tokens
$http({
method: 'POST',
url: YAHOO.requestTokenUrl,
headers: {
Authorization: 'Basic ' + yahooCreadentials,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
redirect_uri: + YAHOO.redirectUrl,
grant_type: 'authorization_code',
code: access_code
}
})
.then(
function(response) {
//accessToken: response.access_token
//refreshToken: response.refresh_token
//expiresIn: response.expires_in
//xoauthYahooGuid: response.xoauth_yahoo_guid
},
function(error) {
//show errors
}
);
}
else {
//show errors
}
});
//using this tokens you can get info from yahoo via api
//for example get user profile
var socialApiUrl = 'https://social.yahooapis.com/v1/user/' + xoauthYahooGuid + '/profile?format=json';
$http({
method: 'GET',
url: socialApiUrl,
headers: {
Authorization: 'Bearer ' + accessToken
}
})
.then(
function(response) {
//response - user profile
},
function(error) {
//show errors
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment