5

After a user authenticaes in our Org using the SSO oauth flow, and after the access token is created, we would like to hook into that flow to get that access token and set a couple of cookies.

Is this possible?

Is there a hook in the Apex code somewhere or can we set the callback URL of the oauth request to our own Apex class to intercept (and in this case, how do we "login" the user)?

-- An alternative if there is no built-in mechanism for us to grab the access token is to define the callback for the "authorization token" to an apex class, and from there make the oauth request for the access token, store that token and then "login" the user.

Im not sure what "login the user" exactly means in salesforce or how to do it.

Does anyone have any sample code that makes the call to get the access token and/or "login" the user.

2

Using Visualforce you can do this. On return the tokens will be in the URL. You will need to set up a connected app but here is a sample controller code:

**Note the actually flow is not depicted here. You will have to create the VF page to manage the flow or add it to the code below. In this test, I had button calling the step 1 method and an action in the page to call the step 2 method

public class oAuth_Controller{

private auth_response rt;

public pagereference auth_Step_1(){


    String auth_url = 'https://login.salesforce.com/services/oauth2/authorize';
    String params =  
                        '?response_type=code' +
                        '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
                        '&redirect_uri=https://na1.salesforce.com/apex/{YOURVFPAGE}' +                           '&prompt=consent' + 
                        '&scope=' + encodingUtil.URLEncode('full refresh_token','UTF-8') +
                        '&state=step2';
        pageReference pr = New PageReference(auth_url + params);
        return pr;
}

public pagereference auth_Step_2(){

    if(apexPages.currentPage().getParameters().get('state') != 'step2')
        return null;


    HttpRequest req = new HttpRequest();
    Http http = new Http();

    String auth_url = 'https://login.salesforce.com/services/oauth2/token';
    String params =  
                        '?code=' + apexPages.currentPage().getParameters().get('code') +
                        '&grant_type=authorization_code' + 
                        '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
                        '&client_secret=YOURSECRET' + 
                        '&redirect_uri=https://na1.salesforce.com/apex/YOURVFPAGENAME';

    req.setMethod('POST');
    req.setEndpoint(auth_url + params);

    HTTPResponse resp = http.send(req);

    rt = (auth_response)json.deserialize(resp.getBody(),auth_response.class);
    //Do something with the results
    return null;

}

public void getnewtoken(){

    HttpRequest req = new HttpRequest();
    Http http = new Http();

    String auth_url = 'https://login.salesforce.com/services/oauth2/token';
    String params =  

                        '?grant_type=refresh_token' + 
                        '&client_id=' + encodingUtil.urlencode('YOURCLIENTID','UTF-8') +
                        '&client_secret=YOURSECRET' + 
                        '&refresh_token=' + encodingUtil.URLEncode(YOURREFRESHTOKEN,'UTF-8');

    req.setMethod('POST');
    req.setEndpoint(auth_url + params);

    HTTPResponse resp = http.send(req);


}



private class auth_response{

    public string refresh_token;
    public string access_token;

}

}

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.