From b60502d8a0b8e8ab0400fe6f63526d6886540f7f Mon Sep 17 00:00:00 2001 From: Maravilho Singa Date: Sun, 22 Mar 2020 17:24:22 +0100 Subject: [PATCH] Instagram: Support passing in API url (#6398) * Update instagram.js Instagram API was updated. * Update instagram.js Instagram API was updated and is not allowing anymore to setup new projects to use the old style but it is still working for the ones that have it already setup. New docs are listed here: https://developers.facebook.com/docs/instagram-basic-display-api/ I've added support for both old and new API To use new API just add new field "api_type" = "new_api" in client side. For old API just no changes needed. * support api url Co-authored-by: Diamond Lewis --- spec/AuthenticationAdapters.spec.js | 35 +++++++++++++++++++++++++++++ src/Adapters/Auth/instagram.js | 30 +++++++++++-------------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/spec/AuthenticationAdapters.spec.js b/spec/AuthenticationAdapters.spec.js index 008decd6..1dfd190e 100644 --- a/spec/AuthenticationAdapters.spec.js +++ b/spec/AuthenticationAdapters.spec.js @@ -590,6 +590,41 @@ describe('AuthenticationProviders', function() { }); }); +describe('instagram auth adapter', () => { + const instagram = require('../lib/Adapters/Auth/instagram'); + const httpsRequest = require('../lib/Adapters/Auth/httpsRequest'); + + it('should use default api', async () => { + spyOn(httpsRequest, 'get').and.callFake(() => { + return Promise.resolve({ data: { id: 'userId' } }); + }); + await instagram.validateAuthData( + { id: 'userId', access_token: 'the_token' }, + {} + ); + expect(httpsRequest.get).toHaveBeenCalledWith( + 'https://api.instagram.com/v1/users/self/?access_token=the_token' + ); + }); + + it('should pass in api url', async () => { + spyOn(httpsRequest, 'get').and.callFake(() => { + return Promise.resolve({ data: { id: 'userId' } }); + }); + await instagram.validateAuthData( + { + id: 'userId', + access_token: 'the_token', + apiURL: 'https://new-api.instagram.com/v1/', + }, + {} + ); + expect(httpsRequest.get).toHaveBeenCalledWith( + 'https://new-api.instagram.com/v1/users/self/?access_token=the_token' + ); + }); +}); + describe('google auth adapter', () => { const google = require('../lib/Adapters/Auth/google'); const httpsRequest = require('../lib/Adapters/Auth/httpsRequest'); diff --git a/src/Adapters/Auth/instagram.js b/src/Adapters/Auth/instagram.js index ee6302c1..4cbeba64 100644 --- a/src/Adapters/Auth/instagram.js +++ b/src/Adapters/Auth/instagram.js @@ -1,20 +1,21 @@ // Helper functions for accessing the instagram API. var Parse = require('parse/node').Parse; const httpsRequest = require('./httpsRequest'); +const defaultURL = 'https://api.instagram.com/v1/'; // Returns a promise that fulfills iff this user id is valid. function validateAuthData(authData) { - return request('users/self/?access_token=' + authData.access_token).then( - response => { - if (response && response.data && response.data.id == authData.id) { - return; - } - throw new Parse.Error( - Parse.Error.OBJECT_NOT_FOUND, - 'Instagram auth is invalid for this user.' - ); + const apiURL = authData.apiURL || defaultURL; + const path = `${apiURL}users/self/?access_token=${authData.access_token}`; + return httpsRequest.get(path).then(response => { + if (response && response.data && response.data.id == authData.id) { + return; } - ); + throw new Parse.Error( + Parse.Error.OBJECT_NOT_FOUND, + 'Instagram auth is invalid for this user.' + ); + }); } // Returns a promise that fulfills iff this app id is valid. @@ -22,12 +23,7 @@ function validateAppId() { return Promise.resolve(); } -// A promisey wrapper for api requests -function request(path) { - return httpsRequest.get('https://api.instagram.com/v1/' + path); -} - module.exports = { - validateAppId: validateAppId, - validateAuthData: validateAuthData, + validateAppId, + validateAuthData, };