0

I am trying to create REST API using apex class in Salesforce. I am following the Link to create REST API. I want to make it public so that we can access this from anywhere only by hitting the URL like: https://myInstance.com/services/apexrest/MyClassURLMappingName.

But it gives me error of authorization or URL not found/URL not correct etc.

Now my question is: How can I get authenticated with Salesforce Instance without giving User name and password?

For this, i think I have to create a connected application. But it required callback URL, (I am confuse about it. What it should be?).

After creating connected app, we have client ID, client Secret. Now what is the code that I need to write in apex class to make REST API link accessible publicly, but without Force.com Sites.

Please help me out with some code or some exact steps. I have create a simple class with some debug statements and I just want to get them in debug logs, to check if my API is working fine? It is authenticating automatically with Salesforce instance or not.

Any help will be appreciated.

  • are you trying to make callout from one salesforce org to another salesforce org? – Ratan Paul Aug 10 '16 at 9:39
  • No, I just want to call that API from any external website, like QQ Catalyst or some javascript code. – Khawaja M. Awais Aug 10 '16 at 9:54
  • 1
    Why not use Force.com Sites? If you don't mind me asking.. – martin Aug 10 '16 at 11:32
  • Because I want to hit that URL from some external system. – Khawaja M. Awais Aug 12 '16 at 6:51
  • Sites can do that for you.. – martin Aug 12 '16 at 9:11
3

It's not possible to create public rest service to salesforce without credentials. You need some kind of service between salesforce and WWW with hardcoded credentials/authToken and refresh token processing strategy (dependant on authentication strategy) e.g. we use Mule esb

But still it's not safe to make salesforce api accesible to everyone.

  • Let'say, I have created a connected app in Salesforce. and I am giving Client ID and Consumer Secret in the auht link to get authorized. Then it will give me authToken. Then If I use this AuthToken in every time when a person want to use our API. Is it right? – Khawaja M. Awais Aug 12 '16 at 6:07
  • Yep, but remember that auth token will time out. It all depends on authentication flow you will choose, look at below link. As you will see here there is no possibility to keep rest service in salesforce public without third party service between. You need some logic responsible for refreshing token. developer.salesforce.com/page/… – KBaluc Aug 12 '16 at 7:51
  • Ok. I understand that we have to refresh AuthToken again and again. So, is there any other way to create Public REST API in salesforce so that we can access it from external systems, like if we want to create new Lead from some external system. How we can do that withouit giving access to our credentails as well as without Force.com Sites? – Khawaja M. Awais Aug 15 '16 at 7:21
0

I've implemented some REST services exposed in Sites, not sure if this fits with your requirements but basically:

  1. You develop the REST class in salesforce
  2. Check you have a site created
  3. In the site public settings (Guess Site profile) you can select the class you want to expose.
  4. The service will be available throw the site URL
  5. You can implement your own security model as token per user / contact or custom object or wither a HMAC header.

You can play around with this. Obviously will be some limits talking about the amount of people it's going to login in the exposed objects and security.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.