REST Client Salesforce to Salesforce
While this won't handle the authorization (oAuth setup) bit, here's a gist that you can use as a basic RestClient. This is a Virtual class, so you'll need to create your own class and extend this rest class. Once you do that, you'll have access to Get(), Put(), Post(), etc. Including the ability to set Oauth and Authorization headers needed for Salesforce Rest API calls.
If you're interested in collaborating on building an oAuth enabled RestClient for SF, let me know.
RestClient Gist
/*End point Url to web service callout*/
private final static String ENP_POINT_URL ='https://login.salesforce.com/services/oauth2/token';
//For development and production https://login.salesforce.com/services/oauth2/token
//And for sandbox https://test.salesforce.com/services/oauth2/token
private final static String CONSUMER_KEY = 'Your_Org_Consumer_Key';
private final static String CONSUMER_SECRET = 'Your_Org_Consumer_Secret';
private final static String USERNAME = 'Your_Username';
private final static String PASSWORD = 'Your_Password';
private final static String REQUEST_BODY = 'grant_type=password&client_id={0}&client_secret=
{1}&username={2}&password={3}';
/*To generate Access token Method*/
private static OAuth getAccessToken(){
try{
HttpRequest req = new HttpRequest();
req.setEndpoint(ENP_POINT_URL);
req.setMethod('POST');
Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setBody(String.format(REQUEST_BODY ,new string[]{CONSUMER_KEY,CONSUMER_SECRET,
USERNAME,PASSWORD}));
req.setTimeout(60000);
Http http = new Http();
HttpResponse res = http.send(req);
OAuth objAuthenticationInfo = (OAuth)JSON.deserialize(res.getbody(), OAuth.class);
return objAuthenticationInfo;
}catch(CallOutException ce){
throw ce;
}
return null;
}
/*To get Access token property*/
public static OAuth authenticationDetail{
get{
if(authenticationDetail == null){
authenticationDetail = getAccessToken();
}
return authenticationDetail;
}set;
}
/*To get aouthentication detail Wrapper*/
public class OAuth{
public String id{get;set;}
public String issued_at{get;set;}
public String instance_url{get;set;}
public String signature{get;set;}
public String access_token{get;set;}
}
}
I agree with above answers but if you need the code just for username-password flow i pulled this to help you