4

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

Code:

    public class AuthCallout {

        public void basicAuthCallout(){
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization','OAuth ');
        req.setHeader('Accept-Encoding','gzip,deflate');
        req.setHeader('Content-Type','application/json');
        //req.setMethod('GET');
        req.setEndpoint('https://api.xero.com/api.xro/2.0/');
        req.setMethod('GET');

        // 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 ' +
         EncodingUtil.base64Encode(headerValue);
         //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);
           System.debug('Xero'+res.getBody());
         }
         Catch(Exception Ex) {
           System.Debug(Ex.getmessage());
         }
    }
}
  • 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 ? – Samuel De Rycke Oct 25 '13 at 11:58
1

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 Sep 25 '16 at 18:06
  • Apologies about that, one of my earlier posts. The line has been removed. – techbusinessman Sep 25 '16 at 23:54
  • My apologies as well, did not notice the date – Eric Sep 26 '16 at 0:51
0

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.

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.