function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Accelerize DemoAccelerize Demo 

Oauth Integration

Hi Pat i am using the same code but oauth integration has not been happened
Accelerize DemoAccelerize Demo
The Code is:


public class OAuthRestController {     static String clientId = '3MVG9ZL0ppGP5UrAhLiH3yCHPS0BmMyI0npsl.dfwapF8Yho0QiwzgSlhs00TMZZ6ejJQSu2jExqswguEzgor'; // Set this in step 3     static String clientSecret = '7284099428900905772'; // Set this in step 3    
 static String redirectUri = 'https://oauthconnect-dev-ed.my.salesforce.com'; // YOUR PAGE URL IN THE SOURCE ORG     static String loginUrl = 'https://oauthconnect-dev-ed.my.salesforce.com'; // YOUR MY DOMAIN URL IN THE TARGET ORG     static String cookieName = 'oauth'; public OAuthRestController() {  
  //getrestTest();    login();
}    
 public PageReference login() {         // Get a URL for the page without any query params         String url = ApexPages.currentPage().getUrl().split('\\?')[0];        
        System.debug('url is '+url);
        String oauth = (ApexPages.currentPage().getCookies().get(cookieName) != null ) ? ApexPages.currentPage().getCookies().get(cookieName).getValue() : null;                     if (oauth != null) {
            // TODO - Check for expired token  
       }              
   System.debug('oauth='+oauth);       
  if (oauth != null) {      
       // All done        
     return null;      
   }                
 // If we get here we have no token       
  PageReference pageRef;        
        if (! ApexPages.currentPage().getParameters().containsKey('code')) {         
    // Initial step of OAuth - redirect to OAuth service           
  System.debug('OAuth Step 1');                
     String authuri = loginUrl+'/services/oauth2/authorize?'+ 'response_type=code&client_id='+clientId+'&redirect_uri='+redirectUri;
                                        pageRef = new PageReference(authuri);    
     } else {  
           // Second step of OAuth - get token from OAuth service     
        String code = ApexPages.currentPage().getParameters().get('code');       
      System.debug('OAuth Step 2 - code:'+code);                     
        String body = 'grant_type=authorization_code&client_id='+clientId+ '&redirect_uri='+redirectUri+'&client_secret='+clientSecret+            '&code='+code;      
       System.debug('body is:'+body);         
                    HttpRequest req = new HttpRequest();        
     req.setEndpoint(loginUrl+'/services/oauth2/token');     
        req.setMethod('POST');      
       req.setBody(body);            
         Http h = new Http();   
          HttpResponse res = h.send(req);    
            String resp = res.getBody();      
       System.debug('FINAL RESP IS:'+EncodingUtil.urlDecode(resp, 'UTF-8'));       
                  ApexPages.currentPage().setCookies(new Cookie[]{new Cookie(cookieName,res.getBody(), null,-1,false)});                             // Come back to this page without the code param             // This makes things simpler later if you end up doing DML here to<br>     
       // save the token somewhere<br>      
      pageRef = new PageReference(url);   
          pageRef.setRedirect(true);       
  }                 return pageRef;  
   }                       
  public void restTest() {    
 String oauth = (ApexPages.currentPage().getCookies().get(cookieName) != null ) ?         ApexPages.currentPage().getCookies().get(cookieName).getValue() : null;     JSONObject oauthObj = new JSONObject( new JSONObject.JSONTokener(oauth));             String accessToken = oauthObj.getValue('access_token').str,        
    instanceUrl = oauthObj.getValue('instance_url').str;     
HttpRequest req = new HttpRequest();   
 req.setMethod('GET');   
  String url = 'https://oauthconnect-dev-ed.my.salesforce.com/services/apexrest/GetService ';          
  req.setEndpoint(url);   
  req.setHeader('Authorization', 'OAuth '+accessToken);   
  Http http = new Http();     HTTPResponse res = http.send(req);  
   System.debug('BODY: '+res.getBody());  
   System.debug('STATUS:'+res.getStatus());    
 System.debug('STATUS_CODE:'+res.getStatusCode());   
     // return res.getBody();
}                          

Page:


<apex:page controller="OAuthRestController" action="{!restTest}">
<h1>Rest Test</h1>
getRestTest says "{!restTest}"
</apex:page>