[JAVA] Get Salesforce data using REST API

Introduction

After reading this article to the end, you will be able to:

--Set up Salesforce API (enable OAuth settings) --Get Salesforce data with Python, Java, Shell Script --Implementing REST web services in Salesforce Apex

Sample code

python_sfdc_get_user.py


sf = Salesforce(username=USERNAME, password=PASSWORD,
                security_token=SECURITY_TOKEN, sandbox=False)
res = sf.query(
    'SELECT Id, Name, LastLoginDate FROM User WHERE Name =\'nsuhara\'')
print(json.dumps(res, indent=4))

Execution result

python_result.json


{
    "totalSize": 1,
    "done": true,
    "records": [
        {
            "attributes": {
                "type": "User",
                "url": "/services/data/v38.0/sobjects/User/0056F000006StK9QAK"
            },
            "Id": "0056F000006StK9QAK",
            "Name": "nsuhara",
            "LastLoginDate": "2019-05-18T13:07:00.000+0000"
        }
    ]
}

Related articles

-SFDC REST API Developer's Guide

Execution environment

environment Ver. Note
macOS Mojave 10.14.4 OS
Salesforce Spring'19 SaaS
Python 3.7.3 Python
simple-salesforce 0.74.2 Python
requests 2.22.0 Python
Java 1.8.0_192 Java
httpclient 4.5.6 Java
json 20180813 Java
jackson-databind 2.9.8 Java

Source code

I think that understanding will deepen if you read while actually following the implementation contents and source code. Please use it by all means.

GitHub

Advance preparation

Salesforce OAuth settings

Settings> Create> Applications> Connected Applications> Click New

item name Set value
Connected application name Sample
API reference name Sample
Contact Email [email protected]
Enable OAuth settings TRUE
Callback URL https://sample.auth0.com/login/callback
Selected OAuth range Access to basic information
Web server flow secret required TRUE
スクリーンショット 2018-12-19 23.42.27.png

`Please wait 2-10 minutes for the changes to take effect on the server before using the connected application. ``

Parameter overview

Variable name Description Remarks
HOST Production: login.salesforce.com, Sandbox: test.salesforce.com
CLIENT_ID Consumer key Reference 1
CLIENT_SECRET Consumer secret Reference 1
USERNAME Salesforce username
PASSWORD Salesforce password
SECURITY_TOKEN Salesforce security token Reference 2
PASSWORD_AND_SECURITY_TOKEN Salesforce password+Security token

** (Reference 1) **

スクリーンショット 2018-12-20 0.29.33.png

** (Ref 2) **

Security tokens can be reissued from My Settings> Personal> Reset My Security Talk.

スクリーンショット 2018-12-20 0.43.24.png

Shell Script sample

shell_script_sfdc_get_user.sh


#!/bin/sh

export HOST='<Parameter reference>'
export CLIENT_ID='<Parameter reference>'
export CLIENT_SECRET='<Parameter reference>'
export USERNAME='<Parameter reference>'
export PASSWORD_AND_SECURITY_TOKEN='<Parameter reference>'

export INSTANCE_URL=`curl -s https://$HOST/services/oauth2/token -d "grant_type=password" -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" -d "username=$USERNAME" -d "password=$PASSWORD_AND_SECURITY_TOKEN" | awk 'BEGIN{FS="instance_url\":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}' | sed -e 's/\"//g'`
export ACCESS_TOKEN=`curl -s https://$HOST/services/oauth2/token -d "grant_type=password" -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" -d "username=$USERNAME" -d "password=$PASSWORD_AND_SECURITY_TOKEN" | awk 'BEGIN{FS="access_token\":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}' | sed -e 's/\"//g'`

export SOQL="SELECT+Id,Name,LastLoginDate+FROM+User+WHERE+Name='nsuhara'"

curl $INSTANCE_URL/services/data/v45.0/query?q=$SOQL -H "Authorization: OAuth $ACCESS_TOKEN" -H "X-PrettyPrint:1"

shell_script_result.json


{
    "totalSize": 1,
    "done": true,
    "records": [
        {
            "attributes": {
                "type": "User",
                "url": "/services/data/v45.0/sobjects/User/0056F000006StK9QAK"
            },
            "Id": "0056F000006StK9QAK",
            "Name": "nsuhara",
            "LastLoginDate": "2019-05-18T14:04:05.000+0000"
        }
    ]
}

Python sample

python_sfdc_get_user.py


import json

from simple_salesforce import Salesforce

USERNAME = '<Parameter reference>'
PASSWORD = '<Parameter reference>'
SECURITY_TOKEN = '<Parameter reference>'


def main():
    sf = Salesforce(username=USERNAME, password=PASSWORD,
                    security_token=SECURITY_TOKEN, sandbox=False)
    res = sf.query(
        'SELECT Id, Name, LastLoginDate FROM User WHERE Name =\'nsuhara\'')
    print(json.dumps(res, indent=4))


if __name__ == '__main__':
    main()

python_result.json


{
    "totalSize": 1,
    "done": true,
    "records": [
        {
            "attributes": {
                "type": "User",
                "url": "/services/data/v38.0/sobjects/User/0056F000006StK9QAK"
            },
            "Id": "0056F000006StK9QAK",
            "Name": "nsuhara",
            "LastLoginDate": "2019-05-18T14:06:19.000+0000"
        }
    ]
}

Java sample

java_App.java


package rest_api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class App {
    static final String HOST = "<Parameter reference>";
    static final String CLIENT_ID = "<Parameter reference>";
    static final String CLIENT_SECRET = "<Parameter reference>";
    static final String USERNAME = "<Parameter reference>";
    static final String PASSWORD_AND_SECURITY_TOKEN = "<Parameter reference>";

    static final String GRANT_SERVICE = "/services/oauth2/token?grant_type=password";

    public static void main(String[] args) {
        String UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36";
        List<Header> headers = new ArrayList<Header>();
        headers.add(new BasicHeader("User-Agent", UA));

        HttpClient httpClient = HttpClientBuilder.create().setDefaultHeaders(headers).build();

        String loginURL = "https://" + HOST + GRANT_SERVICE + "&client_id=" + CLIENT_ID + "&client_secret="
                + CLIENT_SECRET + "&username=" + USERNAME + "&password=" + PASSWORD_AND_SECURITY_TOKEN;

        HttpPost httpPost = new HttpPost(loginURL);
        HttpResponse response = null;

        try {
            response = httpClient.execute(httpPost);
        } catch (ClientProtocolException cpException) {
            // Handle protocol exception
        } catch (IOException ioException) {
            // Handle system IO exception
        }

        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Error authenticating to Force.com: " + statusCode);
            // Error is in EntityUtils.toString(response.getEntity())
            return;
        }

        String getResult = null;
        try {
            getResult = EntityUtils.toString(response.getEntity());
        } catch (IOException ioException) {
            // Handle system IO exception
        }

        JSONObject jsonObject = null;
        String loginAccessToken = null;
        String loginInstanceUrl = null;

        try {
            jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
            loginInstanceUrl = jsonObject.getString("instance_url");
            loginAccessToken = jsonObject.getString("access_token");
        } catch (JSONException jsonException) {
            // Handle JSON exception
        }

        System.out.println(response.getStatusLine());

        httpPost.releaseConnection();
        try {
            final URIBuilder builder = new URIBuilder(loginInstanceUrl);
            final String SOQL = "SELECT Id, Name, LastLoginDate FROM User WHERE Name = \'nsuhara\'";
            builder.setPath("/services/data/v45.0/query/").setParameter("q", SOQL);
            final HttpGet get = new HttpGet(builder.build());
            get.setHeader("Authorization", "Bearer " + loginAccessToken);

            final HttpResponse queryResponse = httpClient.execute(get);

            ObjectMapper mapper = new ObjectMapper();
            final JsonNode queryResults = mapper.readValue(queryResponse.getEntity().getContent(), JsonNode.class);

            System.out.println("queryResults:" + queryResults);
        } catch (Exception e) {
            // Handle exception
        }
    }
}

java_result.json


{
    "totalSize": 1,
    "done": true,
    "records": [
        {
            "attributes": {
                "type": "User",
                "url": "/services/data/v45.0/sobjects/User/0056F000006StK9QAK"
            },
            "Id": "0056F000006StK9QAK",
            "Name": "nsuhara",
            "LastLoginDate": "2019-05-18T14:09:31.000+0000"
        }
    ]
}

REST Web Service Implementation Sample

Apex implementation

apex_SampleRestApi.cls


@RestResource(urlMapping='/sample/restapi/*')
global without sharing class SampleRestApi {
	@HttpGet
	global static List<User> getUsers() {
		return [SELECT Id, Name, LastLoginDate FROM User WHERE Name = 'nsuhara' LIMIT 1];
	}
}

Enable Apex REST service

Settings> Manage Users> Privilege Set (or Profile)> Edit System Privileges

スクリーンショット 2019-05-19 0.17.52.png

Enable Apex class access

Settings> Manage Users> Permission Set (or Profile)> Edit Apex Class Access`

スクリーンショット 2019-05-19 0.18.41.png

HTTP method GET test

python_requests_sfdc.py


import requests

HOST = '<Parameter reference>'
CLIENT_ID = '<Parameter reference>'
CLIENT_SECRET = '<Parameter reference>'
USERNAME = '<Parameter reference>'
PASSWORD_AND_SECURITY_TOKEN = '<Parameter reference>'


def main():
    params = {
        'grant_type': 'password',
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'username': USERNAME,
        'password': PASSWORD_AND_SECURITY_TOKEN
    }

    res_post = requests.post(
        'https://{}/services/oauth2/token'.format(HOST), params=params)

    access_token = res_post.json().get('access_token')
    instance_url = res_post.json().get('instance_url')
    services_url = '/services/apexrest/sample/restapi'
    headers = {
        'Content-type': 'application/json',
        'Accept-Encoding': 'gzip',
        'Authorization': 'Bearer {}'.format(access_token)
    }

    res_get = requests.request(method='get', url=instance_url+services_url,
                               headers=headers, params={'xxx': 'yyy'}, timeout=10)

    print(res_get.status_code)
    print(res_get.json())


if __name__ == '__main__':
    main()

python_result.json


[
    {
        "attributes": {
            "type": "User",
            "url": "/services/data/v45.0/sobjects/User/0056F000006StK9QAK"
        },
        "Id": "0056F000006StK9QAK",
        "Name": "nsuhara",
        "LastLoginDate": "2019-05-18T14: 11: 22.000+0000"
    }
]

Summary-Examination of implementation model

Consider the implementation model based on the environment and conditions

No. Examination conditions Implementation model sample
1 If you have a package for Salesforce Implement using a package Python sample
2 When the execution result of SOQL is sufficient Implement using HTTP request Shell Script sample,Java sample
3 Other than the above Implement Apex REST Web Services REST Web Service Implementation Sample

Recommended Posts

Get Salesforce data using REST API
Get LEAD data using Marketo's REST API in Python
Get Amazon data using Keep API # 1 Get data
Get Youtube data in Python using Youtube Data API
FX data collection using OANDA REST API
[Python] Get all comments using Youtube Data API
How to get article data using Qiita API
[Python] Get insight data using Google My Business API
Data acquisition using python googlemap api
Get data using Ministry of Internal Affairs and Communications API
Data acquisition memo using Backlog API
Get data from Twitter using Tweepy
[Python] I tried to get various information using YouTube Data API!
I tried using YOUTUBE Data API V3
Get mail using Gmail API in Java
Get Google Fit API data in Python
Creating Google Spreadsheet using Python / Google Data API
Awareness of using Aurora Severless Data API
Golang api get
Let's create a REST API using SpringBoot + MongoDB
Get image URL using Flickr API in Python
Get stock price data with Quandl API [Python]
Get data via salesforce API (Bulk API) in Python and load it into BigQuery
[Rails] How to get location information using Geolocation API
Data analysis using xarray
Get Amazon RDS (PostgreSQL) data using SQL with pandas
Data analysis using Python 0
Data cleansing 2 Data cleansing using DataFrame
Data cleaning using Python
I tried APN (remote notification) using Parse.com REST API
Get a list of GA accounts, properties, and views as vertical data using API
Create a pseudo REST API server using GitHub Pages
Get comments and subscribers with the YouTube Data API
I tried using the API of the salmon data project
Get Gzip-compressed data on-memory
Get tweets with arbitrary keywords using Twitter's Streaming API
I tried to search videos using Youtube Data API (beginner)
How to reset password via API using Django rest framework
Obtain vulnerability information using the REST API published by NVD
Hit REST in Python to get data from New Relic
Get data from analytics API with Google API Client for python
[Python] I tried collecting data using the API of wikipedia
Get product name and lowest price using Amazon Product Advertising API
Get the weather using the API and let the Raspberry Pi speak!
Play with YouTube Data API v3 using Google API Python Client
I tried to get data from AS / 400 quickly using pypyodbc
Get structural data from CHEMBLID
Test CloudStack API Using Simulator
Age recognition using Pepper's API
Get similar posts using Doc2Vec
Select features using text data
Get Youtube data with python
Try using the Twitter API
Upload videos using YouTube API
Get information with zabbix api
Try using the Twitter API
Data visualization method using matplotlib (1)
Get GitHub information using PyGithub
Try using the PeeringDB 2.0 API
Data visualization method using matplotlib (2)
Use configparser when using API