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
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.