I need to connect Salesforce with external system using REST API. I am connecting Salesforce with external system using Username and password.

While connecting I get error code : 302

I have attached my code.

I read the documentation but couldn't get any idea to connect this.

Error Message


    public class AuthCallout {

        public void basicAuthCallout(){
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization','OAuth ');

        // Specify the required user name and password to access the endpoint 

        // As well as the header and header information 

         String username = 'xxxxxxx';
         String password = '@@@@@@@@@@';

         Blob headerValue = Blob.valueOf(username + ':' + password);
         String authorizationHeader = 'BASIC ' +
         //req.setHeader('Authorization', authorizationHeader);

         // Create a new http object to send the request object 

         // A response object is generated as a result of the request   

         Http http = new Http();
         try {
           HTTPResponse res = http.send(req);
         Catch(Exception Ex) {
  • 1
    What's the relation to apex and visualforce ? It's a lot easier to find and search questions when the tags are correct. Would you mind elaborating in your question (by edit) why they are relevant, or remove them ? Commented Oct 25, 2013 at 11:58

2 Answers 2


Based on the Xero API documentation (http://developer.xero.com/documentation/api/api-overview/) they mandate authentication using OAuth (v1.0a).

In your sample code you are using BASIC authentication which will not correctly authenticate you and therefore redirect you to a different page.

Please read up on completing an OAuth (v1.0) authentication flow. Here are some great resources to start with:

  1. Salesforce OAuth 1.0 Authentication Flow - https://help.salesforce.com/HTViewHelpDoc?id=remoteaccess_oauth_1_flows.htm&language=en_US
  2. Sundog Interactive walkthrough of completing OAuth using APEX - https://www.sundoginteractive.com/sunblog/posts/using-oauth-1.0-in-salesforce-apex
  3. Another StackExchange question to help further - Using OAuth 1.0 encoded requests for external webservice calls

Hopefully this gets you going in the right direction.

  • please remove the line about marking as answer. Soliciting in your answer for acceptance is frowned upon here. If the op comments that your post solved it and fails to mark it correct then feel free to direct a comment to them to do so so others can find answers
    – Eric
    Commented Sep 25, 2016 at 18:06
  • Apologies about that, one of my earlier posts. The line has been removed. Commented Sep 25, 2016 at 23:54
  • My apologies as well, did not notice the date
    – Eric
    Commented Sep 26, 2016 at 0:51

A 302 error is a redirect (as you may already know). Is it possible that the Http request is malformed and the endpoint is redirecting to an auth required page? You can use a site like: http://requestb.in/ to help debug http requests.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .