Skip to content

Commit

Permalink
Merge pull request #19 from jlobos/challenge-endpoints
Browse files Browse the repository at this point in the history
Added challenge endpoints
  • Loading branch information
timolins authored Mar 19, 2018
2 parents b0bd45d + 091bacd commit 8e4f472
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ const credentialsFile = joinPath(__dirname, 'credentials.json')
* [.getMediaByShortcode({ shortcode })](#getmediabyshortcodeparams)
* [.addComment({ mediaId, text })](#addcommentparams)
* [.deleteComment({ mediaId, commentId })](#deletecommentparams)
* [.getChallenge({ challengeUrl })](#getchallengeparams)
* [.updateChallenge({ challengeUrl, choice, securityCode })](#updatechallengeparams)
* [.resetChallenge({ challengeUrl })](#resetchallengeparams)
* [.replayChallenge({ challengeUrl })](#replaychallengeparams)
* [.approve({ userId })](#approveparams)
* [.ignore({ userId })](#ignoreparams)
* [.follow({ userId })](#followparams)
Expand Down Expand Up @@ -310,6 +314,43 @@ await client.deleteComment({ mediaId: '1442533050805297981', commentId: '1784890
- `mediaId`: The media id
- `commentId`: The comment id

### getChallenge(params)
```js
await client.getChallenge({ challengeUrl: '/challenge/1284161654/a1B2c3d4E6/' })
```
> Get information about a challenge.
- `params`
- `challengeUrl`: A `String` with a challenge path

### updateChallenge(params)
```js
const challengeUrl = '/challenge/1284161654/a1B2c3d4E6/'

await client.updateChallenge({ challengeUrl, choice: 0 })
await client.updateChallenge({ challengeUrl, securityCode: 123456 })
```
> Request or submit a verifcation code for the given challenge.
- `params`
- `challengeUrl`: A `String` with a challenge path
- `choice`: `Number` `0` for phone and `1` for email. Default is ``
- `securityCode`: `Number` the received verifcation code for the challenge. Default is ``

### resetChallenge(params)
```js
await client.resetChallenge({ challengeUrl: '/challenge/1284161654/a1B2c3d4E6/' })
```
> Reset a challenge to start over again.
- `params`
- `challengeUrl`: A `String` with a challenge path

### replayChallenge(params)
```js
await client.replayChallenge({ challengeUrl: '/challenge/1284161654/a1B2c3d4E6/' })
```
> Request a new verifcation message.
- `params`
- `challengeUrl`: A `String` with a challenge path

### approve(params)
```js
await client.approve({ userId: '1284161654' })
Expand Down
46 changes: 45 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ class Instagram {
.then(res => res.find(cookie => cookie.key === 'csrftoken'))
.then(res => res.toJSON())

// Provide CSRFToken for login or challenge request
this.request = this.request.defaults({
headers: { 'X-CSRFToken': value }
})

// Login
const cookies = await this.request
.post('/accounts/login/ajax/', {
headers: { 'X-CSRFToken': value },
resolveWithFullResponse: true,
form: { username, password }
})
Expand Down Expand Up @@ -241,6 +245,46 @@ class Instagram {
return this.request.post(`/web/comments/${mediaId}/delete/${commentId}/`)
}

getChallenge({ challengeUrl }) {
return this.request(`${challengeUrl}?__a=1`)
}

_navigateChallenge({ challengeUrl, endpoint, form }) {
const url = endpoint
? challengeUrl.replace('/challenge/', `/challenge/${endpoint}/`)
: challengeUrl
return this.request.post(url, {
headers: {
Referer: `${baseUrl}${challengeUrl}`
},
form
})
}

updateChallenge({ challengeUrl, choice, securityCode }) {
return this._navigateChallenge({
challengeUrl,
form: {
choice,
security_code: securityCode
}
})
}

resetChallenge({ challengeUrl }) {
return this._navigateChallenge({
challengeUrl,
endpoint: 'reset'
})
}

replayChallenge({ challengeUrl }) {
return this._navigateChallenge({
challengeUrl,
endpoint: 'replay'
})
}

approve({ userId }) {
return this.request.post(`/web/friendships/${userId}/approve/`)
}
Expand Down

0 comments on commit 8e4f472

Please sign in to comment.