Skip to content

Commit

Permalink
Update to use the new graph API
Browse files Browse the repository at this point in the history
  • Loading branch information
ezequielgarcia88 committed Mar 20, 2020
1 parent 9dd2532 commit 8f277bc
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ function Strategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://api.instagram.com/oauth/authorize/';
options.tokenURL = options.tokenURL || 'https://api.instagram.com/oauth/access_token';

OAuth2Strategy.call(this, options, verify);
this.name = 'instagram';
this._clientSecret = options.clientSecret;
this._signRequests = options.signRequests;
this._profileURL = options.profileURL || 'https://graph.instagram.com/me?fields=id,username';
}

/**
Expand All @@ -73,39 +74,19 @@ util.inherits(Strategy, OAuth2Strategy);
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
// TODO: Instagram provides user profile information in the access token
// response. As an optimization, that information should be used, which
// would avoid the need for an extra request during this step. However,
// the internal node-oauth module will have to be modified to support
// exposing this information.

var baseInstagramAPI = 'https://api.instagram.com/v1'
, userInfoEndpoint = '/users/self';

if (this._signRequests) {
// Sign user profile info request according to Instagram API
// This is required if "Force Signed Requests" is enabled in Instaram client's settongs (Under Security)
// https://www.instagram.com/developer/secure-api-requests/
userInfoEndpoint += '?sig=' + crypto.createHmac('sha256', this._clientSecret).update(userInfoEndpoint + '|access_token=' + accessToken).digest('hex');
}

this._oauth2.get(baseInstagramAPI + userInfoEndpoint, accessToken, function (err, body, res) {
this._oauth2.get(this._profileURL, accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }

try {
var json = JSON.parse(body);

var profile = { provider: 'instagram' };
profile.id = json.data.id;
profile.displayName = json.data.full_name;
profile.name = { familyName: json.data.last_name,
givenName: json.data.first_name };
profile.username = json.data.username;
profile.photos = [{value: json.data.profile_picture}];

profile.id = json.id;
profile.username = json.username;

profile._raw = body;
profile._json = json;

done(null, profile);
} catch(e) {
done(e);
Expand Down

0 comments on commit 8f277bc

Please sign in to comment.