diff --git a/index.js b/index.js index f2af593..951189a 100644 --- a/index.js +++ b/index.js @@ -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); + }); }); } \ No newline at end of file