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 <findlewis@gmail.com>
This commit is contained in:
Maravilho Singa
2020-03-22 17:24:22 +01:00
committed by GitHub
parent beecedbdf1
commit b60502d8a0
2 changed files with 48 additions and 17 deletions

View File

@@ -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');

View File

@@ -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,
};