44

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 Credential. "But wait," I hear you say, "you didn't say it wasn't possible." And for that, you'd be correct. Here's what we can do to work within this system's ...


21

adding password manually after retrieve and before deploy worked : <?xml version="1.0" encoding="UTF-8"?> <NamedCredential xmlns="http://soap.sforce.com/2006/04/metadata"> <endpoint>https://prodbox-dev.herokuapp.com/v1</endpoint> <label>prodbox_heroku</label> <principalType>NamedUser</principalType>...


20

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 because it doesn't support the Text (Encrypted) data type doesn't mean it's impossible. Custom Metadata Type / Requires Query In addition to the same comment ...


19

The error occurs if your named credential is configured to use per-user authentication but the currently logged in user that is trying to use the named credential (or their administrator) has not added their credentials to the Authentication Settings for External Systems section (located under My Settings) for that named credential. The error message ...


14

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) has a publicly accessible Social or other major API along the lines of Facebook, Google, Twitter, etc where you can import a token or some other credential to ...


11

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 credential needs to look like this https://bucket-name.s3.amazonaws.com/ Or when you are making the callout append the / in the Apex like this req.setEndpoint('...


10

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 be understood by the server. Your server should NOT be accepting usernames and passwords in the query string, as this is a major security risk.


9

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 will need to skip this prefix in the packaging Dev org. Another source suggests that you need to include the package namespace after Winter '16. Before winter ...


9

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 SF which Named Credential to link the grant against, &scope=full... - list of scopes/privileges presented to the user at the Allow screen, Apex code to ...


8

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.NamedCredential(); credential.fullName = 'Demo_Credential'; credential.label = 'Demo Credential'; credential.endpoint = 'https://www.DEMO2.com'; credential....


8

Ahh, new Salesforce features. That said, if you're mocking, you're not really testing any of that functionality, just how the response is handled. You're also not calling a real server, so the endpoint doesn't really matter. I'm never a fan of having to do hoops around limited Test functionality, but seems like we can make do in the interim. So for the ...


8

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 credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. To simplify the setup of authenticated callouts, ...


8

Unfortunately, you can't. Editing Components and Attributes After Installation has this to say about Named Credentials: Developer Editable Endpoint Label Subscriber and Developer Editable Allow Merge Fields in HTTP Body Allow Merge Fields in HTTP Header Auth Provider Certificate Generate Authorization Header Identity ...


8

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":"INVALID_SESSION_ID"}] I suspect what you've run into is covered by the idea Batch Jobs - Multi User - Named Credential Usage. There is also the corresponding ...


8

Bit of a hack indeed, but there is a way to use Named Credentials to implement the Client Credentials flow so you don't have to manage access or refresh tokens yourself. All credit for figuring this out should go to Chuck Liddell, who did a brilliant Pluralsight course on this topic that I highly recommend (most relevant part to Client Credentials flow ...


7

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 packages namespace. I.e. One method to update the token and another to perform the callout. Once the token is only used from within the managed package you can ...


7

UPDATE for Winter `16 - Simplify Development of Web Service Callouts by Using Named Credentials Short answer. No, it doesn't appear Named Credentials are supported by Wsdl2Apex. I created minimal Named Credential for a public webservicex web service that doesn't require authentication (to check if it will work for the endpoint URL). Then I tried to ...


6

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 salesforce type. Give it the oauth consumer key and consumer secret that you got when you created the connected app. Finally, create a Named Credential using ...


6

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 '401' status code only (Only way for SF to know that the session is expired)


6

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 continue to fail because it cannot find the AuthProvider. If you rename it replacing spaces with underscores (Name and URL Suffix will both have no special chars)...


6

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 Provider to be packaged then redirect url will be same (same as packaging org) for all the subscriber orgs and it will redirect incorrectly to packaging org ...


5

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 path by creating a Connected App and Authorization Provider, but that seems like a lot of work to get something you already have in Apex. Instead, in Apex you ...


5

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:


5

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 DeveloperName, Endpoint FROM NamedCredential


5

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) { AuthProvider provider = [ SELECT Id,OauthKickoffUrl FROM AuthProvider WHERE DeveloperName = :authProvider ]; return ...


5

This line is not needed when using named credentials as callout endpoints: req.setHeader('Authorization','Bearer {!$D365Credential.access_token}'); From the example HttpRequest req = new HttpRequest(); req.setEndpoint('callout:My_Named_Credential/some_path'); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res....


4

when I checked Generate Authorization Header check box on Named Credential then error went off.


4

I had the same issue and managed to solve it by adding req.SetHeader('Accept','application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8');


4

I have done something similar to make a call to the Salesforce Rest API from lightning (well technically Apex). The session that lightning produces is an isolated session that can't be used anywhere else. It is specific to lightning only. In my use case, I needed to get some information about a list view that wasn't exposed in describe calls or querying the ...


4

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 depend on the requirements of the external system The Release Notes May be helpful a would the Documentation


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