0

I am new in salesforce and I have this rest request to get the access token of the other org so that I can get the installed packages. My problem is, I get an error in sending the authorization request.

This is my code:

global class PageOAuth 
{
    global String AuthenticationURI='';
    public PageOAuth(String Clientkey,String redirect_uri)
    {
        String  authuri = 'https://login.salesforce.com/services/oauth2/token?'+
        'grant_type=password'+
        '&client_id=3MV****mac8HelqCcvI1nmj****trEz'+
        '&client_secret=******81'+
        '&username=r*****.com.au'+
        '&password=A******O0n3';
        AuthenticationURI=authuri;
    }
}

,

public class PageController 
{
    private string key = '3MVG9Y*******5Kee_gfUGmac8HelqCcvI1****z' ;
    private string secret = '189030*****';
    private String REQUEST_BODY = 'grant_type=password&client_id=3MVG9******Gmac8HelqCcvI1nmjgZGYjXZfVrfjOLvig.QtrEz&client_secret=1890300580417420481&username=r****.com.au&password=A***4kUf28MPA**3';
    private String USERNAME = 'r*****.com.au';
    private String PASSWORD = 'A****4kUf28M***RPBO0n3';
    private string redirect_uri = 'https://monitorclientsf-dev-ed.lightning.force.com/services/apexrest/api/PackageLicense';
    private String codemeniaIdentificationToken ; 
    private String access_token; 
    private List<String> FileLst;
    public Map<String,String> FileIdAndNameMapFortheAccount{get;set;}

    public PageReference DriveAuth()
    {
        PageReference pg = new PageReference(new PageOAuth(key , redirect_uri).AuthenticationURI) ;
        return pg;
    }

    public void AccessToken()
    {
        //Getting access token from client org
        HttpRequest req = new HttpRequest();
        Blob headerValue = Blob.valueOf(USERNAME + ':' + PASSWORD);
        String authorizationHeader = 'BASIC ' +
        EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader); 
        req.setBody(String.format(REQUEST_BODY ,new string[]{key,secret,
                                                             USERNAME,PASSWORD}));
        req.setTimeout(60*1000);
        Http h = new Http();
        HttpResponse res = h.send(req);
        //Salesforce defined JSON Parser
        JSONParser parser = JSON.createParser(res.getBody());
        while (parser.nextToken() != null) 
        {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'access_token'))
            {
                parser.nextToken();
                access_token=parser.getText();
                break;
            }
        }
        if (access_token!='' && access_token != null)
        {
            ListFiles();
        }
    }

public PageReference ListFiles()
{  
    Client_Org_Information__c g=new Client_Org_Information__c();
    g=[SELECT client_id__c FROM Client_Org_Information__c];
    String FolderId=g.client_id__c;
    HttpRequest req1 = new HttpRequest();
    req1.setMethod('GET');
    //End point for List of Files in Folder
    req1.setEndpoint('https://monitorclientsf-dev-ed.lightning.force.com/services/apexrest/api/PackageLicense');
    req1.setHeader('content-type', 'application/x-www-form-urlencoded');
    req1.setHeader('Content-length', '0');
    //Specific Authorization Syntax
    req1.setHeader('Authorization' , 'Bearer'+' '+ access_token);
    req1.setTimeout(60*1000);
    Http h2 = new Http();
    HttpResponse resl = h2.send(req1);
    System.debug('^^^^^'+resl.getBody());
    JSONParser parserD = JSON.createParser(resl.getBody());
    while (parserD.nextToken() != null) 
    {
        if ((parserD.getCurrentToken() == JSONToken.FIELD_NAME) && (parserD.getText() == 'id'))
        {
            parserD.nextToken();
            FileLst.add(parserD.getText());
        }
    }
    Map<String,String> FileIdAndNameMap=FileProperties();
        for(String s:FileLst)
        {
            FileIdAndNameMapFortheAccount.put(s, FileIdAndNameMap.get(s));
        }
        return null;
}
public  Map<String,String> FileProperties()
{
    Map<String,String> FilePropertiesDetails=new Map<String,String>();
    HttpRequest req2 = new HttpRequest();
    req2.setMethod('GET');
    //End point URL for All the files in the drive and its ID,Name value in JSON Response
    req2.setEndpoint('https://monitorclientsf-dev-ed.lightning.force.com/services/apexrest/api/PackageLicense');
    req2.setHeader('content-type', 'application/x-www-form-urlencoded');
    req2.setHeader('Content-length', '0');
    req2.setHeader('Authorization' , 'Bearer'+' '+ access_token);
    req2.setTimeout(60*1000);
    Http h3 = new Http();
    HttpResponse resls = h3.send(req2);
    System.debug('^^^^^'+resls.getBody());
    JSONParser parserDr = JSON.createParser(resls.getBody());
    while (parserDr.nextToken() != null) 
    {   
        String Name='';
        String id='';
        if ((parserDr.getCurrentToken() == JSONToken.FIELD_NAME))
        {
            //First TOKEN is Id
            if((parserDr.getText() == 'id'))
            {
                parserDr.nextToken();               
                id=parserDr.getText();
            }
            //Moving to Next Parser
            parserDr.nextToken();
            //Next Token is Name
            if((parserDr.getText() == 'name'))
            {
                parserDr.nextToken();
                Name=parserDr.getText();
            }
            FilePropertiesDetails.put(id, Name);
        }
    }
    return FilePropertiesDetails;
  }
}

My VisualForce Page

<apex:page controller="PageController">
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton value="Send Request" action="{!DriveAuth}"/>
            <apex:pageBlockTable value="{!FileIdAndNameMapFortheAccount}" var="f">
                <apex:column headerValue="Installed Packages Files">
                    <apex:outputLink value="https://monitorclientsf-dev-ed.lightning.force.com/services/apexrest/api/PackageLicense?id={!f}" target="_blank">{!FileIdAndNameMapFortheAccount[f]}</apex:outputLink>
                </apex:column>
            </apex:pageBlockTable>
         </apex:pageBlock>
    </apex:form>
</apex:page>

Error Message:

<OAuth> <error>invalid_request</error> <error_description>must use HTTP POST</error_description> </OAuth>

Can anyone help me...

2
  • Please add error message as well.
    – Mr.Frodo
    Aug 2, 2018 at 5:31
  • ' <OAuth> <error>invalid_request</error> <error_description>must use HTTP POST</error_description> </OAuth>' Aug 2, 2018 at 5:39

1 Answer 1

0

You will need to set the method for the callout to 'POST'. According to the error message it expects a POST but you are sending a GET

Change:

req2.setMethod('GET');

To:

req2.setMethod('POST');
3
  • I already change GET to POST but the error's still the same Aug 2, 2018 at 5:51
  • What about you AccessToken method? It doesn't seem to have it set at all. Aug 2, 2018 at 5:52
  • Is my code almost there? I'm so sorry, I am new in Salesforce and I badly wanted to retrieve the records in other salesforce org Aug 2, 2018 at 5:53

You must log in to answer this question.

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