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
Akshita SinghAkshita Singh 

Auto Refresh Access Token

I have written this method to get accesstoken when I send Id & password given(Which I havent Hardcoded Below). I am saving my access token in custom setting and want it to expire, call this method again and save a new token at 12:01am every night. Any suggestions?

 
public static string Login(){ HttpRequest req = new HttpRequest(); req.setEndpoint('xyz'); req.setMethod('POST'); req.setHeader('Content-Type', ''); req.setBody('{ "id" : "+Id+", "password" : "+Password+" }');

 Http http = new Http();
 HTTPResponse res = http.send(req);
 System.debug(res.getBody());

 if(res.getstatuscode()==200)
 {
     Map<string,string> result=(map<string,string>) 
   JSON.deserializeUntyped(res.getbody());
     access_token =string.valueOf(result.get(access_token));
    AccessToken__c At = AccessToken__c.getorgdefaults();
     at.Token__c = access_token;
 }

 
Best Answer chosen by Akshita Singh
Khan AnasKhan Anas (Salesforce Developers) 
Hi Akshita,

Greetings to you!

Your app needs permissions (scope) to have the ability to use refresh tokens.
In Setup > Quick Find > App Manager >, click the "Edit" link for your Connected App and add the scope "Perform requests on your behalf at any time (refresh_token, offline_access)".

Once this has saved (you may have to wait a while), you will be able to change the value for the refresh token policy.

Reference: https://salesforce.stackexchange.com/questions/69161/refresh-token-policy-locked-to-immediatly-expire-token

Also, please refer to below links which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/65590/what-causes-a-connected-apps-refresh-token-to-expire

https://salesforce.stackexchange.com/questions/73512/oauth-access-token-expiration

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Akshita,

Greetings to you!

Your app needs permissions (scope) to have the ability to use refresh tokens.
In Setup > Quick Find > App Manager >, click the "Edit" link for your Connected App and add the scope "Perform requests on your behalf at any time (refresh_token, offline_access)".

Once this has saved (you may have to wait a while), you will be able to change the value for the refresh token policy.

Reference: https://salesforce.stackexchange.com/questions/69161/refresh-token-policy-locked-to-immediatly-expire-token

Also, please refer to below links which might help you further with the above issue.

https://salesforce.stackexchange.com/questions/65590/what-causes-a-connected-apps-refresh-token-to-expire

https://salesforce.stackexchange.com/questions/73512/oauth-access-token-expiration

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Akshita SinghAkshita Singh
Thankyou Anas, I will go through the links as well. 
Appreciate your help.