I created Named Credentials to authorize the api and got the token. Now i want to use the same end point to get the weatherdetail. when i tried to access same named credential then its showing error as invalid access token.
Token URL http://XXXXXX.com/tocken
WeatherDetailUrl http://xxxxxx.com/zipcode
Any idea to reuse the named credential else i have to create another remote action and custom setting for this.
Code:
public string gettocken(){
HttpRequest request = new HttpRequest();
Http http = new Http();
request.setEndpoint('callout:WeatherAPI');
request.setMethod('GET');
request.setHeader('Content-length', '0');
HTTPResponse response = http.send(request);
Map<String,Object> data = (Map<String,Object>)JSON.deserializeUntyped(response.getBody());
return (String)data.get('access_token');
}
public void weatheCall() {
string token = gettocken();
HttpRequest request = new HttpRequest();
Http http = new Http();
request.setEndpoint('callout:WeatherAPI'+'/TX');
request.setMethod('GET');
request.setHeader('Content-length', '0');
request.setHeader('Authorization', 'Bearer '+token);
HTTPResponse response = http.send(request); **///Error {"fault":{"faultstring":"Invalid access token","detail":{"errorcode":"oauth.v2.InvalidAccessToken"}}}**
system.debug(response.getbody());
}