0

I am trying to implement Web Server Authentication Flow.I have two orgs , source and destination.In destination org I have created connected app and rest resource where I am using post method of rest api.In the source org I have a apex class, from where the rest api method is being called.I am successfully able to get the authorization code by redirecting to some vf page.Like this

'https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id='+CLIENT_ID+'&redirect_uri=https://c.ap2.visual.force.com/apex/DemoPage';

Code is appended to the redirect_uri after the redirection.Using this code I am requesting for the access token, using http request.

string reqbody = 'grant_type=authorization_code&code='+codeparam+'&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&redirect_uri=https://c.ap2.visual.force.com/apex/DemoPage';

    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setBody(reqbody);
    req.setMethod('POST');
   req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

In the returned response,the instance url is of the source org(from where the rest api service is called).But I want the instance url of destination org(where rest resource is present).Because of this i am getting "message":"Could not find a match for URL /createChild/" error.Please help me with this.

Thanks

0

You should try to use the "named credential" feature: https://help.salesforce.com/apex/HTViewHelpDoc?id=named_credentials_about.htm&language=en_US

It can help to handle automatically the authentication, instead of implementing it manually in the Apex code. With the named credential you can call the web services of the other org without taking care of the authentication protocol (Oauth2 here).

  • I tried using named credentials.Now getting this error - " The callout couldn't access the endpoint. You might not have the required permissions, or the named credential might not exist."I have enabled the access for named credential for system admin profile. – Smitha Aug 5 '15 at 12:28
  • Could you verify that your endpoint is allowed in your org (see Remote Settings)? Add it if it's not yet done. – Abdel-Ali B. Aug 5 '15 at 12:36
  • It is allowed.I have added it. – Smitha Aug 5 '15 at 12:47
  • You do not need to add a remote site if a named credential exist for the same endpoint. Can you share Apex code as well for better understanding? – the_phantom Mar 5 '18 at 21:14

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.