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
Shuhbam SinhaShuhbam Sinha 

How to generate access token and use in POST method

Hello,
I have one requirement to use POST method for one rest api. Requirement is to generate the access token first and then use it on post method. I have user name, password and end point to generate the access token and i am able to generate through postman as well but I am not sure how to do it in apex and that access token to use in my post methood to hit one end point. Could anyone please help me on this. Thanks in advane.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Shuhbam,

Can you check if below link helps you to solve it.

https://stackoverflow.com/questions/43549104/how-to-get-the-access-token-first-in-apex-restful-service-to-call-get-post-metho

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Shuhbam SinhaShuhbam Sinha
Thankyou for the reply but I need to create one separate method to get the access token and then use it on the post method which has separate method and  in body of this method i am sending some json data as well.
home decoretodayhome decoretoday
Store the access token securely: Once you have obtained the access token, securely store it in a variable or database. Treat the access token as sensitive information since it provides authorization to access protected resources.
Use the access token in the POST method: Include the access token in the authorization header of your POST request. The exact method for adding the access token to the request will depend on the programming language or framework you are using. Typically, you would set the "Authorization" header to "Bearer {access_token}".
Send the POST request: Build and send the POST request to the desired endpoint, including any required parameters or data in the request body.
Handle the response: Receive and process the response returned from the API after making the POST rod Baitcasting (https://gearknacks.com/best-baitcasting-rods/). The response will contain the result of the operation you performed.
Zoni GillZoni Gill

Sure, I can help you with that! In Apex, you can use the HttpRequest class to make HTTP requests, including the POST method to generate an access token. Here's a general outline of the steps you can follow:
Use the HttpRequest class to create a POST request to the endpoint that generates the access token. Set the necessary headers, such as "Content-Type" for the request body and "Authorization" if you need to provide basic authentication (username and password).
Send the request using the HTTP class and capture the response.
Parse the response to extract the access token from the JSON or any other format it returns.
Once you have the access token, you can use it in your subsequent POST method to hit the desired endpoint. Simply set the "Authorization" header with the value "Bearer [your_access_token]"
Remember to handle any exceptions that might occur during the process, and also make sure to keep your sensitive information (like passwords) secure by using secure methods like custom settings, named credentials, or protected custom metadata.
I'd be happy to provide more specific assistance if you share a specific code snippet or more details about your implementation. Good luck!