Salesforce rest API logon | authenticating to Salesforce using rest, oauth 2.0 and Java

Source: Internet
Author: User

This tutorial will show you how you can authenticate to Salesforce using restful services.

Configure remote access in Salesforce

 

The first thing to do is to allow external applications to connect to Salesforce. external applications will be recognized by two values-client_id and client_secret. in order to generate them, go to setup | develop | remote access. on the list of remote access applications, click New to create an entry for your external app.

 

Fill in the form with proper values-they can be whatever you like. We will be using a username and password scenario in oauth, so the callback URL field can also have any value whatsoever.

 

Once you're done filling the form, click "save ". A screen will show up with details of your application. on the bottom of the page there is a section called "authentication ", and there you can find the values of client_id and client_secret which will be of interest to us in the later part of the tutorial.

Calling the login service with rest

 

In order to obtain an access token, we will send an http post request to the authentication endpoint exposed by Salesforce. If you are using your production organization, the endpoint's address will be:

Https://login.salesforce.com/services/oauth2/token

 

If you are connecting to a developer sandbox, use:

Https://test.salesforce.com/services/oauth2/token

 

The complete code to send to request is:

Postmethod method =NewPostmethod ("https://login.salesforce.com/services/oauth2/token ");

 

Httpmethodparams Params =NewHttpmethodparams ();

 

Stringbuilder content =NewStringbuilder ();

Content. append ("grant_type = password ");

Content. append ("& client_id = response ");

Content. append ("& client_secret = 24953483493483500 ");

Content. append ("& username = user@domain.com ");

Content. append ("& Password = <password> <security-Token> ");

 

Method. setrequestentity (NewStringrequestentity (content. tostring (), "text/plain", "UTF-8 "));

Method. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");

 

Httpclient client =NewHttpclient ();

Client.exe cutemethod (method );

 

One thing to remember is that the parameter password is in fact a concatenated value of your password and your security token. it is also essential to set the Content-Type of the request to application/X-WWW-form-urlencoded.

 

As a result of this call you shoshould obtain the following response:

{

"ID": "https://login.salesforce.com/id/00D50000000IZ32WKW/00550000001fjHSwW ",

"Issued_at": "1296458209517 ",

"Refresh_token": "5aep862ewo5d. 7wjbuw5aaarbbxsdju3qi59o1du7ob. lp23ba_3jmrnbfnt5r8x2gukna = ",

"Instance_url": "https://na5.salesforce.com ",

"Signature": "0/1 ldval/tipf2ttdjsjkksdki3899g ",

"Access_token": "00d50000000iz3z! Else"

}

 

You are authenticated. To authorize your CILS, use the access_token value encoded ed in the response.

 

 

 

Http://wordgraphs.com/post/853

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.