function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
theDreamertheDreamer 

oauth consumer key and/or signature not included in request parameters or authorization header

I am calling a REST Api which needs oAuthkey and Password. Here is my Class
 
public with sharing class DetailButtonController
{
  public string responseText {get;set;}

  public DetailButtonController() {
     responseText = '';
  } 

   public void doSomething() {

    String oauthkey='asdasdasd asd77cf6ef73b77';
    String oauthsecret='asdasdasdasd2195385b9aa2';
    String action='get';

    Http m_http = new Http();
    HttpRequest req = new HttpRequest();

    String content = 'action='+EncodingUtil.urlEncode(action, 'UTF-8')+'&oauth_consumer_key='+EncodingUtil.urlEncode(oauthkey, 'UTF-8')+'&oauth_consumer_secret='+EncodingUtil.urlEncode('oauthsecret', 'UTF-8');

    req.setEndpoint('http://www.abc.com/api/1.0/accounts/2323.xml/');
    req.setHeader('Content-Type','application/x-www-form-urlencoded');        
    req.setMethod('POST');
    req.setBody(content);

    httpResponse response = m_http.send(req);

    responseText = response.getBody();
  }

}

Apex Code
<apex:page controller="DetailButtonController">
    <b>Hello World!</b>
    <apex:form >
        <apex:commandButton value="Do something" action="{!doSomething}"/>
        <apex:outputPanel layout="block">
          {!responseText}
        </apex:outputPanel>
    </apex:form>
</apex:page>

The error i am getting is as below, Does that mean there is some other method to call Rest API using oauth?
 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><response success="false" xmlns="http://www.abc.com"><error http-status="401">oauth consumer key and/or signature not included in request parameters or authorization header</error></response>


Best Answer chosen by theDreamer
jyothsna reddy 5jyothsna reddy 5
Hi ,
  ​Please try the below code
String reqbody='grant_type=password&client_id=xxxx &client_secret=xxxx&username=xxxx&password=xxxx';

                Http h = new Http();
                HttpRequest req = new HttpRequest();
                req.setBody(reqbody); 
                req.setMethod('POST');
                req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
                req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
                HttpResponse res = h.send(req);
                                
     
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class); 
                
   if(objAuthenticationInfo.access_token != null){
  
                  
       String pretty = Json.serialize(bt); 
                                        
                    

       Http h1 = new Http();
       
      HttpRequest req1 = new HttpRequest();                    
      
      req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
     
      req1.setHeader('Content-Type','application/json; charset=UTF-8');
    
       req1.setHeader('Accept','application/json');


    
       req1.setBody(pretty);  
    
    

       req1.setMethod('POST');
    
    
       
req1.setEndpoint('https://ap1.salesforce.com/services/apexrest/s2/BottleRest/');
                                                   


       
HttpResponse res1 = h1.send(req1);

I hope it will help you 

Regards,
Jyothsna D
 

All Answers

pconpcon
Are you suppose to be passing these as a form POST body, or are you suppose to be passing this as a URL GET parameter?
theDreamertheDreamer
The API here https://www.gliffy.com/products/online/api/apidocs/rest/#oauth says its POST 
pconpcon
The docs say that you can GET instead of POST.
 
... while DELETE and GET methods MUST include parameters as part of the query string

I would try modifying your setEndpoint to have them as GET parameters instead and see if that helps.
jyothsna reddy 5jyothsna reddy 5
Hi ,
  ​Please try the below code
String reqbody='grant_type=password&client_id=xxxx &client_secret=xxxx&username=xxxx&password=xxxx';

                Http h = new Http();
                HttpRequest req = new HttpRequest();
                req.setBody(reqbody); 
                req.setMethod('POST');
                req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
                req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
                HttpResponse res = h.send(req);
                                
     
    OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(res.getbody(), OAuth2.class); 
                
   if(objAuthenticationInfo.access_token != null){
  
                  
       String pretty = Json.serialize(bt); 
                                        
                    

       Http h1 = new Http();
       
      HttpRequest req1 = new HttpRequest();                    
      
      req1.setHeader('Authorization','Bearer '+objAuthenticationInfo.access_token);
     
      req1.setHeader('Content-Type','application/json; charset=UTF-8');
    
       req1.setHeader('Accept','application/json');


    
       req1.setBody(pretty);  
    
    

       req1.setMethod('POST');
    
    
       
req1.setEndpoint('https://ap1.salesforce.com/services/apexrest/s2/BottleRest/');
                                                   


       
HttpResponse res1 = h1.send(req1);

I hope it will help you 

Regards,
Jyothsna D
 
This was selected as the best answer