Wednesday, June 8, 2011

Using Salesforce.com's REST API from Perl

Introduction

Here's a simple way to access Salesforce data even from behind a firewall, using Perl and the REST API.

If you're not familiar with the details of the Salesforce REST API, have a look at the developer docs.

I'm using Perl 5.12.1 with modules WWW::Mechanize, and WWW::Salesforce.

The firewall settings (optional)

Since I'm behind a firewall, I have to define the necessary variables so the various Perl packages can route the requests properly.  Here's what's needed:
 #!/usr/bin/perl
use warnings;
use strict;
use WWW::Mechanize;
use WWW::Salesforce;

# The firewall proxy variables:
$ENV{HTTPS_PROXY} = 'http://proxy.org.com:80';
$ENV{HTTPS_PROXY_USERNAME} = "proxy_user";
$ENV{HTTPS_PROXY_PASSWORD} = "proxy_pass";
my $mech = WWW::Mechanize->new();
$mech->agent('Mozilla/5.0');
$mech->proxy('http', 'http://proxy_user:proxy_pass@proxy.org.com:80');
$mech->proxy('https', undef);

If you don't need to go through a firewall, you can skip all of these proxy settings, but you'll still need the $mech definition, as it will be used to make the REST requests later.

Authorization

The most difficult part (and it wasn't difficult) was setting up the authorization.  Salesforce offers two options:
  1. OAuth 2.0
  2. Session ID
The Sesion ID is far easier than the first option, so I'm using it here.

The first step is to log-in via the SOAP API (since that is a way to get a session ID):

# Authenticate first via SOAP interface to get a session ID:
my $sforce = eval { WWW::Salesforce->login(
                    username => 'sfdc_user@org.com',
                    password => 'sfdc_pass' ); };
die "Could not login to SFDC: $@" if $@;

Session ID

Now that we've logged-in via the SOAP API, we have a session ID as part of the $sforce variable, which we can get as follows:
 # Get the session ID:
my $hdr = $sforce->get_session_header();
my $sid = ${$hdr->{_value}->[0]}->{_value}->[0];

This SID will need to be sent as part of our request header when using the REST API.

A REST Request

Here's a simple request for all available SObjects:
# Now get some data (using the session ID from above):
$mech->add_header( "Authorization" => "OAuth $sid" );
$mech->add_header( "X-PrettyPrint" => '1' );
$mech->get("https://na1.salesforce.com/services/data/v20.0/sobjects/
");
# ...and write it out to the screen:
print $mech->content;

Going further

 The developer doc mentioned above lists lots of examples for requests that can be made this way, including SOQL queries.

18 comments:

  1. Miguel,

    Great article, many thanks for posting it. I am doing some work with Salesforce and would like to automate the load process using the module however I am at a loss in understanding how or where you specify the environment you need to connect to. Mine is https://test.salesforce.com. If you could help out it would be greatly appreciated.

    Paul

    ReplyDelete
    Replies
    1. Hi Paul,

      Sorry for the delayed reply. Unfortunately, the target URL is hard-coded into WWW::Salesforce, so you'd need to tweak the module code. Have a look at the source here: http://cpansearch.perl.org/src/PHRED/WWW-Salesforce-0.20/lib/WWW/Salesforce.pm.

      The $SF_PROXY variable defines the target URL. You'd need to try changing the "www" to "test", and see if it works. I successfully ran the code shown above from the production side of SFDC, and did not try a test URL as you suggested.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. You can simply override the package variable because it's exposed (it's not a "my" variable). Just set it with

    $WWW::Salesforce::SF_PROXY = "https://$salesforce_domain/services/Soap/u/8.0";

    ReplyDelete
  4. Salesforce CRM likewise offers improved customization abilities to organizations as the cloud-based condition makes it simple for organizations to test any alterations or changes before proceeding with definite usage process. salesforce for nonprofits

    ReplyDelete
  5. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
    CRM Software| CRM System | CRM | Best CRM Software

    ReplyDelete
  6. It is truly a well-researched content and excellent wording. I got so engaged in this material that I couldn’t wait to read. I am impressed with your work and skill. Thanks. Salesforce Admin and Development Training

    ReplyDelete
  7. The Salesforce training online can even be pursued by individuals who have already obtained their DEV 401 certification from Salesforce. Salesforce training in Chennai

    ReplyDelete
  8. With special privileges and services, UEFA BET offers opportunities for small capitalists. Together ufa with the best websites that collect the most games With a minimum deposit starting from just 100 baht, you are ready to enjoy the fun with a complete range of betting that is available within the website

    ufabet , our one another option We are a direct website, not through an agent, where customers can have great confidence without deception The best of online betting sites is that our Ufa will give you the best price

    ReplyDelete
  9. By far the most serious football betting UFABET that's over and above description Find fun, excitement and excitement with slot games, 100 totally free recognition, rapid withdrawal. If you desire to relax slots for cash No requirement to deposit a great deal, with no bare minimum, with no requirement to talk about, waste moment simply because UFABET is actually reduced, paid greatly, a number of great offers are actually waiting for you. Willing to guarantee enjoyable, no matter if it's Joker SlotXo fruit slot, we can telephone call it a web slot site for you personally especially. Prepared to relax Including the support team which is going to facilitate slot formulas along with strategies of enjoying So you will be sure that each minute of exhilaration and fun We will be there for someone to offer the customers of yours the greatest impression and total satisfaction.
    บาคาร่า
    ufa
    ufabet
    แทงบอล
    แทงบอล
    แทงบอล

    ReplyDelete
  10. ได้โดยที่จะทำให้คุณนั้นสามารถสร้างกำไรจากการเล่นเกมส์เดิมพันออนไลน์ได้เราแนะนำเกมส์ชนิดนี้ให้คุณได้รู้จักก็เพราะว่าเชื่อว่าทุกคนนั้นจะต้องรู้วิธีการเล่นและวิธีการเอาชนะเกมม สล็อต าแทบทุกคนเพราะเราเคยเล่นกันมาตั้งแต่เด็กเด็กหาคุณได้เล่นเกมส์คาสิโนออนไลน์ที่คุณนั้นคุ้นเคยหรือจะเป็นสิ่งที่จะทำให้คุณสามารถที่จะได้กำไรจากการเล่นเกมได้มากกว่าที่คุณไปเล่นเกมส์คาสิโนออนไลน์ที่คุณนั้นไม่เคยเล่นมาก่อนและไม่คุ้นเคย เราจึงคิดว่าเกมส์ชนิดนี้เป็นเกมส์ที่น่าสนใจมากๆที่เราอยากจะมาแนะนำให้ทุกคนได้รู้จักและได้ใช้บริการ

    ReplyDelete
  11. You can pay for just about anything with a credit card today, even your mortgage or rent. It's only natural to wonder what else you can pay with a credit card – like property taxes.
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com

    ReplyDelete
  12. The pricing plan is very simple and with the basic option at $8, ads will be removed. On my test with Weebly, I built out a 100-page website which it coped with very well. Using Wix I would not be so confident in building larger sites. If you or somebody on your team is experienced and knows code, Weebly easily allows you to edit the coding. This is great news for advanced users.
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com

    ReplyDelete
  13. A penalty APR may be triggered if you’re at least 60 days late on your credit card payment and your issuer has sent you a written notice of the rate increase at least 45 days before the effective date. The average penalty APR for all cards in the U.S. News study is 27.54 percent.

    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com

    ReplyDelete
  14. More broadly, a tax shelter can be anything used to minimize a person's income tax liability. That means tax shelters can include strategies for deducting expenses to lower your adjusted gross income or paying for workplace benefits with pretax dollars.

    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com

    ReplyDelete
  15. If you drive to volunteer at your favorite nonprofit, that mileage is deductible as part of your charitable donations. The IRS allows volunteers to claim 14 cents per mile, but you have to be volunteering yourself. You can't, for example, be driving a child to a volunteer activity. There is no threshold requirement for claiming these miles.

    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com

    ReplyDelete
  16. You can found a professional institute who are famous for Salesforce Online Training in Ameerpet, Hyderabad, India. Are you really interested to make your career as Salesforce Consultant? Then take a chance to interact with us, our Salesforce Online Training will surely make a difference. Salesforce training in Hyderabad

    ReplyDelete
  17. Bitcoin's creator, Satoshi Nakamoto, originally meant for it to be a form of currency, a way to pay for goods and services to make transactions without middlemen to decrease the cost and transaction times. On the other hand, Ether, Ethereum's native cryptocurrency, powers many other blockchain applications.

    Politics
    Reviews
    Science
    Software
    Sports
    Technology
    Tours & Travels
    Apps
    Business

    ReplyDelete