56 votes
Accepted

Use a Named Credential with API key?

Well... sort of. The only two officially supported methods are by Basic Authentication and OAuth tokens. That site uses a non-standard API design, so it can't directly benefit from using a Named ...
sfdcfox's user avatar
  • 490k
20 votes
Accepted

Where to Store Token Auth?

Your assessments are pretty close, but you've got some glaring errors I'd like to bring up. Custom Settings / No Encryption? You can encrypt the token using the Crypto class if you need to. Just ...
sfdcfox's user avatar
  • 490k
16 votes
Accepted

External + Named Credential with oAuth client credentials flow

At the protocol level, Client Credentials grant is designed to not require a user identity. It involves an application exchanging its application credentials, such as client ID and client secret, for ...
identigral's user avatar
  • 7,556
15 votes
Accepted

Can I use Named Credential merge fields as URL parameters on the endpoint?

You can only use merge fields within setHeader and setBody, as the options specify. This may mean that you need to use POST with application/x-www-form-urlencoded, or arrange for a specific header to ...
sfdcfox's user avatar
  • 490k
14 votes
Accepted

Named Credentials and support for the OAuth2 Client Credentials Grant Type and alternatives

In my experience, the bullet points you've highlighted are pretty much on-target. What I would add is that both NamedCredentials and CustomApplications, make the assumption the OAuth provider (the idP)...
crmprogdev's user avatar
13 votes

Named Credentials and support for the OAuth2 Client Credentials Grant Type and alternatives

EDIT: As of Winter '24, Client Credentials flow is natively supported by the "new" Named Credentials construct. The below workaround is no longer necessary. Bit of a hack indeed, but there ...
James Quinn's user avatar
13 votes

Accessing AWS S3 via a REST API (Apex callout with a Named Credential)

Okay so the answer is actually pretty straightforward so am leaving it here for anybody else struggling with the same issue. The endpoint needs to have a trailing / so either your URL in the named ...
Devst's user avatar
  • 233
13 votes
Accepted

Named credential not accessible by non system admin profiles

Ok I found out the root cause : the profile was missing Read access on the object User External Credentials.
Chris's user avatar
  • 316
12 votes
Accepted

named credential problem

Is this part of a managed package. If so you will need to prefix the named credential with __ (two underscores) to indicate that you are using a named credential from the local namespace. Source. You ...
Daniel Ballinger's user avatar
11 votes

error=redirect_uri_mismatch&error_description=redirect_uri%20must%20match%20configuration

I had the same problem. I found that callback url in the connected app should be the same as you have in your redirect_uri. I tested it with a tool called postman, maybe that can help you as well.
ChristianJuul's user avatar
10 votes
Accepted

Initiate an oAuth flow via an Auth Provider

The flow can be invoked from a user-facing process. Here are the parameters: /services/auth/xds/{org_id}/{authprovider_developer_name} - this issues the logout, &startURL=/0XA... - this ID tells ...
Matt and Neil's user avatar
9 votes

Where should we store username/password/endpoint…in Salesforce?

Better to use Named Credentials Named Credentials are a safe and secure way of storing authentication data for external services called from your apex code such as authentication tokens A named ...
Santanu Boral's user avatar
9 votes
Accepted

Creating/updating NamedCredential from Apex

You need to add two required "fields" to your request: principalType and protocol (you can see the entire list of fields here) Script MetadataService.NamedCredential credential = new MetadataService....
Fernando Gavinho's user avatar
9 votes
Accepted

Session Id not getting refreshed automatically with Custom Auth provider in Salesforce Named Credentials

Just an update for anyone facing the same issue. After raising a case with SF we could confirm that the refresh function is only triggered to get new session Id in case the previous callout return '...
Abhijeet Bhandari's user avatar
8 votes
Accepted

Is it posibble to edit Named Credential url in a managed package?

Unfortunately, you can't. Editing Components and Attributes After Installation has this to say about Named Credentials: Developer Editable Endpoint Label Subscriber and Developer ...
sfdcfox's user avatar
  • 490k
8 votes
Accepted

session is not valid for use with the REST API

Issue 1: When i am trying to execute the Batch class.I am getting following error {"message":"This session is not valid for use with the REST API","errorCode":"...
Daniel Ballinger's user avatar
7 votes

Where to Store Token Auth?

It might be too much work, but you could put all the code that needs to call the external service with the token into a managed package. Then just expose global methods as required from the managed ...
Daniel Ballinger's user avatar
7 votes
Accepted

Why can't we package custom AuthProvider in Managed Package

Auth Provider is org specific. It has a callback URL where a connected app redirects after authentication and org get authcode and complete the rest authentication steps. If salesforce allows Auth ...
Sushant Srivastava's user avatar
7 votes
Accepted

Deploy Permission Set Mapping on External Credential

As of V58.0, you can't deploy External Credential Principal Access for either Permission Sets or Profiles For example, the Metadata API for PermissionSet allows for the deployment of: ...
cropredy's user avatar
  • 71.3k
7 votes
Accepted

External Credential - Unable to use on Digital Experience

As far as I can see, Named Credential mapping does not work with Permission Set Groups. The workaround was the assign the permission set directly to the user. I have found this Idea. I hope this was ...
Jeferson Chaves's user avatar
6 votes

Using Named Credentials to get Salesforce sessionId

You can do this, but you'll need to do a bit more work to use it. First, Create a connected app with oAuth, select refresh_token (at a min) for the oAuth scope Second, Create an auth provider of the ...
Kevin P's user avatar
  • 7,208
6 votes

sfdx force:source:pull -> Entity of type 'AuthProvider' named '' cannot be found

Make sure your AuthProvider name has no spaces. I was able to pull the metadata using the retrieve command: sfdx force:source:retrieve -m AuthProvider However, your subsequent pull commands will ...
berardo's user avatar
  • 61
6 votes

Initiate an oAuth flow via an Auth Provider

If all you need to do is to kick off the oAuth flow so that the user may authorize the app, the auth provider supports this natively: public PageReference (String authProvider) { ...
identigral's user avatar
  • 7,556
6 votes
Accepted

AWS S3 - implementing CRUD operations with Named Credentials

AWS S3 has many interfaces that can be used for integration. One of the options is the S3 REST API. From the API doc: Requests to Amazon S3 can be authenticated or anonymous. Authenticated access ...
identigral's user avatar
  • 7,556
5 votes
Accepted

Salesforce REST API Named Credential INVALID_SESSION_ID

Salesforce doesn't support direct Password Authentication to use the REST API. Typically you would go through an OAuth flow to get a valid access token to then call the service. You could go down this ...
Daniel Ballinger's user avatar
5 votes
Accepted

UserName and Password in Request body of apex callout using Named Credentials

When using merge fields you need to actually set the body like string bdy = '{ "username" : "{!$Credential.Username}", "password" : "{!$Credential.Pasword}"}' req.setBody(bdy); The format will ...
Eric's user avatar
  • 54.2k
5 votes
Accepted

Use Named Credentials with a SOAP WSDL2APEX class

I haven't tested this, but when you are creating your Named Credential you have the option to tick "Generate Authorization Header", which should do this for you automatically:
smukov's user avatar
  • 5,553
5 votes
Accepted

How to manage "Named Credential" with APEX

NamedCredential can be read via soql but you can not directly insert it in to this objects. One workaround for this is use metadata API and Post following request to your org. <?xml version="1.0" ...
AtulRajguru9's user avatar
  • 9,120
5 votes
Accepted

Getting URL from a named credential

According to the SOAP API Developer Guide, the NamedCredential object has a field named Endpoint: Description The root URL of the endpoint. So you should be able to do a query like: SELECT ...
Adrian Larson's user avatar
  • 150k
5 votes
Accepted

What fields deploy with NamedCredential?

A retrieve of Named Credential metadata does not include the sensitive field data, including password. That said, password data can be included in metadata during the deployment and the password ...
Mark Pond's user avatar
  • 23k

Only top scored, non community-wiki answers of a minimum length are eligible