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.


public string gettocken(){
  HttpRequest request = new HttpRequest();
            Http http = new Http();
            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.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"}}}**


1 Answer 1


If the FQDN and/or the relative URI of the token endpoint and your target API (WeatherDetailUrl) are common, you can define a single Named Credential and use it for both calls. This assumes that you're not negotiating the token in your Named Credential (authentication protocol is set to No Authentication)

Single Named Credential named Foo with URL set to http://foo.bar.com/blah will work if

  • Token endpoint is http://foo.bar.com/blah/token
  • Weather API is http://foo.bar.com/blah/weather

Apex code:

request.setEndpoint('callout:Foo'+'/token'); // token endpoint
request.setEndpoint('callout:Foo'+'/weather'); // Weather API

If the Weather API requires a token and the token protocol is supported out of the box by a Named Credential, you can implement it in a single Named Credential. This approach works even if the token endpoint and your target API endpoint do not have a common FQDN.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .