2

I want to develop a chrome extension but i have a problem that how to login in salesforce when click on button . First i made manifest file and then open popup it's working correctly. In this popup two text field one for username and other for password so now i want to login in salesforce when click on button.

  • Are you running this extension on pages outside the salesforce domain or only in salesforce domain? If You're running it only in the salesforce then you can use the sessionId stored in the browser cookie to make REST callouts. But if you're using on pages outside salesforce domain then you can authneticate using oAuth2.0. – Sam Jan 9 '15 at 7:31
  • I am running this extension outside salesforce environment and i want javascript code to do so. I used connection.js but i can't success I am getting many error like unsafe user agent . so tell me how i can do it. – Ravikant Kedia Jan 12 '15 at 4:00
0

Use the chrome.identity API methods. For example:

chrome.identity.launchWebAuthFlow(options, function(redirectUri) {
            if (chrome.runtime.lastError) {
                return callback(new Error(chrome.runtime.lastError));
            }

            // Upon success the response is appended to redirectUri, e.g.
            // https://{app_id}.chromiumapp.org/provider_cb#access_token={value}&refresh_token={value}
            var matches = redirectUri.match(redirectRe);
            if (matches && matches.length > 1) {
                handleProviderCodeResponse(parseRedirectFragment(matches[1]));
            } else {
                callback(new Error('Invalid redirect URI'));
            }
        });

        function parseRedirectFragment(fragment) {
            var pairs = fragment.split(/&/);
            var values = {};

            pairs.forEach(function(pair) {
                var nameVal = pair.split(/=/);
                values[nameVal[0]] = nameVal[1];
            });

            return values;
        }

        function handleProviderCodeResponse(values) {
            if (values.hasOwnProperty("code")) {
                var url = host + '/services/oauth2/token' +
                    '?client_id=' + clientId +
                    '&client_secret=' + clientSecret +
                    '&grant_type=authorization_code' +
                    '&code=' + values.code +
                    '&redirect_uri=' + encodeURIComponent(redirectUri);

                var xhr = new XMLHttpRequest();
                xhr.open("POST", url, true);
                xhr.onload = function() {
                    if (this.status =300) {
                        callback(new Error('error in handleCodeResponse.'));
                    } else {
                        handleProviderTokensResponse(JSON.parse(this.response));
                    }
                };
                xhr.send();
            } else {
                callback(new Error('error in handleProviderCodeResponse.'));
            }
        }

        function handleProviderTokensResponse(values) {
            if (values != null) {
                callback(null, values);
            } else {
                callback(new Error('error in handleProviderTokensResponse.'));
            }
        }
    };

    module.refreshToken = function(connection, callback) {
        var url = connection.instance_url + '/services/oauth2/token?client_id=' + clientId +
            '&client_secret=' + clientSecret +
            '&grant_type=refresh_token' +
            '&refresh_token=' + connection.refresh_token;

        var xhr = new XMLHttpRequest();
        xhr.open("POST", url, true);
        xhr.onload = function() {
            if (this.status =300) {
                callback(new Error('error in handleCodeResponse.'));
            } else {
                handleProviderTokenResponse(JSON.parse(this.response));
            }
        };
        xhr.send();

        function handleProviderTokenResponse(values) {
            if (values.hasOwnProperty('access_token')) {
                callback(null, values);
            } else {
                callback(new Error('error in handleProviderTokenResponse.'));
            }
        }
    };

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.