Skip to content

Commit

Permalink
only resolve url in getAuthorizationUrl,
Browse files Browse the repository at this point in the history
fix function parameters,
update authorization example
  • Loading branch information
vanita5 committed Apr 7, 2017
1 parent 60c49c1 commit 855f1d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Optional. Defines the scopes of your OAuth app whitespace seperated. Defaults to

Optional. Defaults to `urn:ietf:wg:oauth:2.0:oob`. This will be used in a future call to `Mastodon.getAuthorizationUrl(...)`, only the URL defined here can be used later to redirect the user. The default means no redirect (the code will be shown to the user).

## `Mastodon.getAuthorizationUrl(clientId, clientSecret, { baseUrl, scope, redirectUri })`
## `Mastodon.getAuthorizationUrl(clientId, clientSecret, baseUrl, scope, redirectUri)`
Returns an authorization url for users to authorize your application.
`clientId` and `clientSecret` can be obtained by calling `Mastodon.createOAuthApp(...)` before.

Expand All @@ -68,7 +68,7 @@ Your `client_secret`.

Optional. Defaults to `https://mastodon.social`.

**scopes**
**scope**

Optional. Defines the scopes of your OAuth app whitespace seperated. Defaults to `read write follow`.

Expand Down
22 changes: 14 additions & 8 deletions examples/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,32 @@ const rl = readline.createInterface({
output: process.stdout
})

let clientId
let clientSecret

Mastodon.createOAuthApp()
.catch(err => console.error(err))
.then((res) => {
console.log('Please save \'id\', \'client_id\' and \'client_secret\' in your program and use it from now on!')
console.log(res)
return Mastodon.getAuthorizationUrl(res.client_id, res.client_secret)

clientId = res.client_id
clientSecret = res.client_secret

return Mastodon.getAuthorizationUrl(clientId, clientSecret)
})
.then((data) => {
.then(url => {
console.log('This is the authorization URL. Open it in your browser and authorize with your account!')
console.log(data.url)
console.log(url)
return new Promise((resolve) => {
rl.question('Please enter the code from the website: ', (code) => {
data.code = code
resolve(data)
rl.question('Please enter the code from the website: ', code => {
resolve(code)
rl.close()
})
})
})
.then(data => Mastodon.getAccessToken(data.clientId, data.clientSecret, data.code))
.then(code => Mastodon.getAccessToken(clientId, clientSecret, code))
.catch(err => console.error(err))
.then((accessToken) => {
.then(accessToken => {
console.log(`This is the access token. Save it!\n${accessToken}`)
})
6 changes: 3 additions & 3 deletions src/mastodon.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ class Mastodon {
}

static getAuthorizationUrl(clientId, clientSecret,
{ baseUrl = DEFAULT_REST_BASE,
baseUrl = DEFAULT_REST_BASE,
scope = 'read write follow',
redirectUri = 'urn:ietf:wg:oauth:2.0:oob' }) {
redirectUri = 'urn:ietf:wg:oauth:2.0:oob') {
return new Promise((resolve) => {
const oauth = new OAuth2(clientId, clientSecret, baseUrl, null, '/oauth/token')
const url = oauth.getAuthorizeUrl({
Expand All @@ -335,7 +335,7 @@ class Mastodon {
client_id: clientId,
scope
})
resolve({ clientId, clientSecret, url })
resolve(url)
})
}

Expand Down

0 comments on commit 855f1d4

Please sign in to comment.