Skip to content

Commit

Permalink
Fix getAccessToken function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcsz committed Feb 7, 2017
1 parent aaacc6e commit 84ab73f
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ exports.authorizationCodeUri = (appCredentials) => {
reject('Not enough information to build authorization URL.');
}
if (appCredentials.responseType !== undefined) {
fulfill(`${appCredentials.uri.authroization}?client_id=${encodeURI(appCredentials.client.id)}&response_type=${encodeURI(appCredentials.responseType)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`);
fulfill(`${appCredentials.uri.authorization}?client_id=${encodeURI(appCredentials.client.id)}&response_type=${encodeURI(appCredentials.responseType)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`);
} else if (appCredentials.responseType === undefined) {
fulfill(`${appCredentials.uri.authroization}?client_id=${encodeURI(appCredentials.client.id)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`);
fulfill(`${appCredentials.uri.authorization}?client_id=${encodeURI(appCredentials.client.id)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`);
}
});
}

exports.getAccessToken = (authorizationCode, appCredentials) => {
let authorizationHeader = encode.base64Encode(`${appCredentials.client.id}:${appCredentials.client.secret}`);
exports.getAccessToken = (app) => {
return new Promise ((fulfill, reject) => {
let encodedClientCredentials = encode.base64Encode(`${app.client.id}:${app.client.secret}`);

fetch(appCredentials.uri.token, {
method: 'POST',
header: {
'Authorization': `Basic ${authorizationHeader}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `code=${authorizationCode}&grant_type=authorization_code&redirect_uri=${appCredentials.uri.redirect}`
fetch(app.uri.authorization, {
method: 'POST',
header: {
'Authorization': `Basic ${encodedClientCredentials}`,
'Content-Type': 'application/x-ww-form-urlencoded'
},
body: `client_id=${app.client.id}&grant_type=authorization_code&redirect_uri-&${encodeURI(app.uri.redirect)}&code=${app.code}`
}).then((token) => {
fulfill(token.json());
}).catch((error) => {
reject(error);
});
});
}

0 comments on commit 84ab73f

Please sign in to comment.