0

I have two orgs, A and B.

Org A has some exposed REST services. Here is the code:

@RestResource(urlMapping='/pokemon/*')
global class PokemonHelper {

    @httpGet
    global static Pokemon__c getPokemon(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String pokemonId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        return [SELECT ID, Name FROM Pokemon__c WHERE ID = :pokemonId];
    }
    
    @httpPost
    global static String postPokemon(String name) {
        Pokemon__c poke = new Pokemon__c();
        poke.Name = name;
        insert poke;
        return poke.Id;
    }
}

Org B will consume the services from Org A. I have created a connected app in Org A and retrieved the clientId and client secret. I also have my security token for an integration user. Here is the code for Org B:

public class pokemonConsumer {

    
    public static void getPokemon(String pokeId){
        String accessToken = oauthLogin();
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setHeader('Authorization','Bearer ' + accessToken);
        req.setHeader('Content-type', 'application/json;charset=UTF-8');
        
        
        req.setEndpoint('https://curious-wolf-t1kffu-dev-ed.trailblaze.lightning.force.com/services/apexrest/pokemon/' + pokeId);
        Http http = new Http();
        HttpResponse res = http.send(req);
        while(res.getStatusCode() == 302) {
            req.setEndpoint(res.getHeader('Location'));
            System.debug(res.getHeader('Location'));
            res = new Http().send(req);
            System.debug(res);
        }
    
            
        
    }
    
    
    public static void postPokemon(){
        String accessToken = oauthLogin();
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setHeader('Content-type', 'application/json');
        
        req.setHeader('Authorization','Bearer ' + accessToken);
        req.setEndpoint('https://curious-wolf-t1kffu-dev-ed.trailblaze.lightning.force.com/services/apexrest/pokemon/');
        req.setBody('{"Name": "TESTPOKE"}');
        Http http = new Http();
        HttpResponse res = http.send(req);
        if(res.getStatus() == '201') {
            System.debug(res);
        } else {
            System.debug(res.getStatusCode());
            System.debug(res.getBody());
        }
    }
    
    
    public static String oauthLogin() {
        HttpRequest req = new HttpRequest(); 
 
        req.setMethod('POST');
        req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.setHeader('Accept', 'application/json');
        
        req.setBody('grant_type=password' +
            '&client_id=' + 'CLIENT_ID_HERE' +
            '&client_secret=' + 'CLIENT_SECRET_HERE' +
            '&username=USER_NAME_HERE' +
            '&password=USER_PASSWORD_HERE_SECURITY_TOKEN_HERE');
    
        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 parseResponseForAccessToken(res.getBody());
    }
    
    private static String parseResponseForAccessToken(String responseBody) {
        String accessToken;

        JSONParser parser = JSON.createParser(responseBody);
        while (parser.nextToken() != null) {
          if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token')) {
            parser.nextToken();
            accessToken = parser.getText();
            }
          }
        return accessToken;
    }
}

I can get the session ID from Org A just fine. But calls to my exposed services will not work. At first I was getting a 302 status code from my getPokemon() method. I noticed it was redirecting with 'my' in the URL. I added the loop to handle the redirect but now getting a 500 error code...

I've added my endpoints to remote site settings in Org B. Both the original endpoint and the redirected endpoint with 'my' in the URL.

Any thoughts? I'm at a loss and any help is appreciated!

Edit: These are two trailhead playgrounds for extra context. I'm just experimenting with REST at the moment.

Edit 2: Should also mention I'm executing anon apex from the dev console to test this. Maybe there is some limitation of anon apex or playgrounds that pertain to this? I'm not sure. My google-fu has been failing me.

4
  • Does the running user connecting to Org A have permission to apex class PokemonHelper ?
    – cropredy
    Commented Sep 30, 2023 at 18:15
  • They do, I made a perm set for this.
    – Ethan
    Commented Sep 30, 2023 at 20:21
  • Caller org: change the endpoint used in a callout to https://curious-wolf-t1kffu-dev-ed.trailblaze.my.salesforce.com/... You can wrap the entire sequence of acquiring the token + callout to the API in a Named/External Credential.
    – identigral
    Commented Oct 2, 2023 at 23:38
  • Yup that did it. Just needed to start with the my domain the whole time. Thank you all!
    – Ethan
    Commented Oct 2, 2023 at 23:58

0

You must log in to answer this question.

Browse other questions tagged .