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
Kathryn BullockKathryn Bullock 

JSON token

Hi All,
I am trying to update the info on a Visualforce page from the REST API from the site.  I was given a bearer token and I used Postman to get the access token.  I am confused because there are no errors, but the code isn't working.  How do I make this code work?
 
public class AccountShippingController {
   
    public String trackingnumber {get;set;}
    public String shipmentStatus {get;set;}
    public String shipperAddr {get;set;}
    public String consigneeAddr {get;set;}

    public AccountShippingController(ApexPages.StandardController stdController){
                    
        			Account account =(Account)stdController.getRecord();
                    account = [SELECT Id, XPO_Tracking__c FROM Account WHERE Id =:account.Id];
                    
                    String accountTracking = account.XPO_Tracking__c;
                    String apiKey = 'XXXXXXXX';
                    
                    String requestEndpoint = 'https://api.ltl.xpo.com/tracking/1.0/shipments/shipment-status-details';
                    requestEndpoint += '?referenceNumbers[]=' + accountTracking;
        			requestEndpoint += '&APPID=' + apiKey;
                    
                    Http http = new Http();
                    HttpRequest request = new HttpRequest();
                    request.setEndpoint(requestEndpoint);
                    request.setMethod('GET');
                    HttpResponse response = http.send(request);
                    
                    if (response.getStatusCode() == 200) {
                        
                        Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                        trackingnumber = String.valueOf(results.get('referenceNumbers'));
                        
                        Map<String, Object> shipmentStatusDtlsResults = (Map<String, Object>)(results.get('shipmentStatusDtls'));
                        shipmentStatus = String.valueOf(shipmentStatusDtlsResults.get('shipmentStatus'));
                        shipperAddr = String.valueOf(shipmentStatusDtlsResults.get('shipperAddr'));
                        consigneeAddr = String.valueOf(shipmentStatusDtlsResults.get('consigneeAddr'));
                    } else {
                        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an error retrieving the information.  Double check the Tracking Number, then contact a system administrator.');
                        ApexPages.addMessage(myMsg);
                        
                    }
                }
            }
 
<apex:page standardController="Account" extensions="AccountShippingController" showHeader="false" sidebar="false">
           <apex:pageBlock title="Shipping Information">
    			<apex:pageBlockSection >
                
                    <apex:pageMessages />
               
                    <apex:outputText label="Shipping Status" value="{!shipmentStatus}"/>
                    <apex:outputText label="Shipper Information" value="{!shipperAddr}"/>
                    <apex:outputText label="Consignee Information" value="{!consigneeAddr}"/>
                                                                                                            
                </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

 
Best Answer chosen by Kathryn Bullock
Narender Singh(Nads)Narender Singh(Nads)
Hi Kathryn,

For every request you make to that API, you will have to pass that token in the header of the request.
Token is nothing but the sessionId which gets generated when you login in to your account of that website.
Every token has a certain has time limit before it expires. Once expired, you will have to either refresh your token using a refresh token URL, or you can simply do OAuth process login to get a new token.

I hope I'm able to explain myself.
Let me know if there is any doubt.
Thanks!

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi Kathryn,
I think you have to pass the access token in the header while making API requests.
Try using this code:
public class AccountShippingController {
   
    public String trackingnumber {get;set;}
    public String shipmentStatus {get;set;}
    public String shipperAddr {get;set;}
    public String consigneeAddr {get;set;}

    public AccountShippingController(ApexPages.StandardController stdController){
                    
        			Account account =(Account)stdController.getRecord();
                    account = [SELECT Id, XPO_Tracking__c FROM Account WHERE Id =:account.Id];
                    
                    String accountTracking = account.XPO_Tracking__c;
                    String apiKey = 'XXXXXXXX';
                    string AccessToken='xxxxxxxxxxxxxxxxxxxxxxxxx'; // PASS YOUR ACCESS TOKEN HERE
                    
                    String requestEndpoint = 'https://api.ltl.xpo.com/tracking/1.0/shipments/shipment-status-details';
                    requestEndpoint += '?referenceNumbers[]=' + accountTracking;
        			requestEndpoint += '&APPID=' + apiKey;
                    
                    Http http = new Http();
                    HttpRequest request = new HttpRequest();
                    request.setEndpoint(requestEndpoint);
                    request.setMethod('GET');
                    request.setheader('Authorization','Bearer '+AccessToken);
                    HttpResponse response = http.send(request);
                    
                    if (response.getStatusCode() == 200) {
                        
                        Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                        trackingnumber = String.valueOf(results.get('referenceNumbers'));
                        
                        Map<String, Object> shipmentStatusDtlsResults = (Map<String, Object>)(results.get('shipmentStatusDtls'));
                        shipmentStatus = String.valueOf(shipmentStatusDtlsResults.get('shipmentStatus'));
                        shipperAddr = String.valueOf(shipmentStatusDtlsResults.get('shipperAddr'));
                        consigneeAddr = String.valueOf(shipmentStatusDtlsResults.get('consigneeAddr'));
                    } else {
                        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an error retrieving the information.  Double check the Tracking Number, then contact a system administrator.');
                        ApexPages.addMessage(myMsg);
                        
                    }
                }
            }
Please make sure you are passing the right token in the request header.

That should do the trick.

Let me know if it helps
Thanks
Kathryn BullockKathryn Bullock
Thank you for the help!  I was only given the bearer token though, does this mean I will have to go through the Oauth process?
Narender Singh(Nads)Narender Singh(Nads)
Hi Kathryn,

For every request you make to that API, you will have to pass that token in the header of the request.
Token is nothing but the sessionId which gets generated when you login in to your account of that website.
Every token has a certain has time limit before it expires. Once expired, you will have to either refresh your token using a refresh token URL, or you can simply do OAuth process login to get a new token.

I hope I'm able to explain myself.
Let me know if there is any doubt.
Thanks!
This was selected as the best answer