mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-26 13:19:49 +07:00
* Remove docs folder. This was a redirect from a GitHub pages site that does not appear to be in use. * Rename api folder to api-samples. * Move examples to functional-samples folder. * Move cookbook sample to functional-samples. * Move tutorials to functional-samples folder. * Move mv2 and apps folders to _archive. * Rename tools folder to .repo. * Move reference folder to functional-samples. * Update README. Update README with new relative links for reorg. * Update README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
var foursquare = {};
|
|
|
|
(function(api) {
|
|
// See "Pure AJAX application" from
|
|
// https://developer.foursquare.com/overview/auth
|
|
var ACCESS_TOKEN_PREFIX = '#access_token=';
|
|
|
|
var storage = chrome.storage.local;
|
|
var ACCESS_TOKEN_STORAGE_KEY = 'foursquare-access-token';
|
|
|
|
var getAccessToken = function(callback) {
|
|
storage.get(ACCESS_TOKEN_STORAGE_KEY, function(items) {
|
|
callback(items[ACCESS_TOKEN_STORAGE_KEY]);
|
|
});
|
|
}
|
|
|
|
var setAccessToken = function(accessToken, callback) {
|
|
var items = {};
|
|
items[ACCESS_TOKEN_STORAGE_KEY] = accessToken;
|
|
storage.set(items, callback);
|
|
}
|
|
|
|
var clearAccessToken = function(callback) {
|
|
storage.remove(ACCESS_TOKEN_STORAGE_KEY, callback);
|
|
}
|
|
|
|
// Tokens state is not exposed via the API
|
|
api.isSignedIn = function(callback) {
|
|
getAccessToken(function(accessToken) {
|
|
callback(!!accessToken);
|
|
});
|
|
};
|
|
api.signIn = function(appId, clientId, successCallback, errorCallback) {
|
|
var redirectUrl = chrome.identity.getRedirectURL();
|
|
var authUrl = 'https://foursquare.com/oauth2/authorize?' +
|
|
'client_id=' + clientId + '&' +
|
|
'response_type=token&' +
|
|
'redirect_uri=' + encodeURIComponent(redirectUrl);
|
|
chrome.identity.launchWebAuthFlow(
|
|
{url: authUrl, interactive: true},
|
|
function(responseUrl) {
|
|
if (chrome.runtime.lastError) {
|
|
errorCallback(chrome.runtime.lastError.message);
|
|
return;
|
|
}
|
|
|
|
var accessTokenStart = responseUrl.indexOf(ACCESS_TOKEN_PREFIX);
|
|
|
|
if (!accessTokenStart) {
|
|
errorCallback('Unexpected responseUrl: ' + responseUrl);
|
|
return;
|
|
}
|
|
|
|
var accessToken = responseUrl.substring(
|
|
accessTokenStart + ACCESS_TOKEN_PREFIX.length);
|
|
|
|
setAccessToken(accessToken, successCallback);
|
|
});
|
|
};
|
|
api.signOut = function(callback) {
|
|
clearAccessToken(callback);
|
|
};
|
|
|
|
var apiMethod = function(
|
|
path, postData, params, successCallback, errorCallback) {
|
|
getAccessToken(function(accessToken) {
|
|
var xhr = new XMLHttpRequest();
|
|
// TODO(mihaip): use xhr.responseType = 'json' once it's supported.
|
|
xhr.onload = function() {
|
|
successCallback(JSON.parse(xhr.responseText).response);
|
|
}
|
|
xhr.onerror = function() {
|
|
errorCallback(xhr.status, xhr.statusText, JSON.parse(xhr.responseText));
|
|
}
|
|
|
|
var encodedParams = [];
|
|
for (var paramName in params) {
|
|
encodedParams.push(encodeURIComponent(paramName) + '=' +
|
|
encodeURIComponent(params[paramName]));
|
|
}
|
|
xhr.open(
|
|
'GET',
|
|
'https://api.foursquare.com/v2/' + path + '?oauth_token=' +
|
|
encodeURIComponent(accessToken) + '&' + encodedParams.join('&'),
|
|
true);
|
|
xhr.send(null);
|
|
});
|
|
}
|
|
|
|
api.getRecentCheckins = apiMethod.bind(api, 'checkins/recent', undefined);
|
|
})(foursquare);
|