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
Salesforce_NikitaSalesforce_Nikita 

HTTP Callout from Salesforce

Hi there,

I have been trying to get response from Google Analytics API. I have the access token. Can you please suggest the correct syntax of embedding the token in GET Response

Thanks in advance
Grazitti TeamGrazitti Team
Hi Nikita,

You can refer the below code to embed the token in your GET call.

        Http h = new Http();
        HttpRequest req = new HttpRequest();
        string endPointValue = 'https://www.googleapis.com/calendar/v3/calendars/primary/events';   // Your End Point URL     
        
        req.setEndpoint(endPointValue);            
        req.setHeader('Authorization', 'Bearer ' + ACCESS_TOKEN);      
        req.setMethod('GET');
        req.setTimeout(10000);     
        HttpResponse res = h.send(req);


Regards,
Grazitti Team
Salesforce_NikitaSalesforce_Nikita
Hi Grazitti Team,

I have tried this but I am getting Status Code as 403 (Forbidden). Also my endPoint URL has parameters associated to it

Thanks and Regards
Grazitti TeamGrazitti Team
Hi Nikita,

You can aapend the parameters in the setBody() as follows:
     
       
HttpRequest req = new HttpRequest();
       string bodyRequest = '';
        bodyRequest = 'param1=' + paramVal;
        bodyRequest += '&param2 =' + paramVal2;
        
        req.setBody(bodyRequest);


and it seems you have to add "https://www.googleapis.com" in your remote site settings of salesforce org to fix the Status code 403 .

Hope it helps you.

Regards,
Grazitti Team


Salesforce_NikitaSalesforce_Nikita
Hi Grazitti,

I am still getting System.HttpResponse[Status=Not Found, StatusCode=404]. Remote Site settings for URL is active.

Thanks ans Regards
Grazitti TeamGrazitti Team
Hi ,

This might be due to some problem in your access token that you are using to fetch the data (access token might be expired).

Thanks!