In the salesforce REST API documentation, it tells how the system can give Oauth Tokens (https://code.exacttarget.com/apis-sdks/rest-api/v1/auth/requestToken.html ) But I don't seen any documentation on how your system receives Oauth tokens when sending data.

I'm fairly new at combining OAuth with REST API, and so this is screwing with me a bit.

This code successfully gives me a token:

$exactTarget = 'https://auth.exacttargetapis.com/v1/requestToken';
$OAUTHarray = array('clientId' => 'SomethingGoesHere', 'clientSecret' => 'sshhhNowThatisASecret');

$options = array(
        'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $OAUTHarray ),
                'header'=>  "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n"

$context  = stream_context_create( $options );
$result = file_get_contents( $exactTarget, false, $context );
$response = json_decode( $result );

I've outputted the data to make sure I was getting a valid token (and I was.) However, when trying to actually run commands, I can't get the tokens accepted.

This is my current follow-up code:

$exactTarget = 'https://www.exacttargetapis.com/contacts/v1/contacts';
$values = array ('name' => '' . $name, 'value' => '' . $email);
$items = array ('values' => $values);
$attributeSet = array ('items' => $items);
$dataset01 = array('contactKey' => '' . $email, 'contactId' => null, 'attributeSets' => $attributeSet);

$valuesArray = array($lastname, $firstname);
$itemsArray = array('values' => $valuesArray);
$dataset02 = array('name' => 'Email Democgraphics', 'items' => $itemsArray);
$finalData = array($dataset01, $dataset02);

if ($debug == true){
    echo "<script>alert(" . $response->{"accessToken"} . ")</script>";

$options = array(
        'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $finalData ),
                'header'=>  "Content-Type: application/json\r\n" .
                "Accept: application/json\r\n" .
                "Authorization: Bearer " . $response->{"accessToken"} . "\r\n"



$context  = stream_context_create( $options );
$result = file_get_contents( $exactTarget, false, $context );
$response2 = json_decode( $result );

So, what's wrong here, or what needs to be added?

  • What $result is being returned? Also, dump the response headers with var_dump($http_response_header); and add them to your question and it should be easier to answer. – metadaddy Feb 3 '15 at 4:51
  • @JoshuaPeterson did you ever figure this out? I'm having the same problem – garek007 May 2 at 21:29
  • @garek007 Unfortunately, no. – Joshua Petersen May 2 at 22:41
  • are you using guzzle? – garek007 May 3 at 14:02
  • @garek007 It's been about 2 years since I worked the job that prompted that question, I can barely remember which frameworks I was using. Sorry. I just know I was never able to pull it off though. – Joshua Petersen May 3 at 16:23

I would suggest using the Fuel SDK, it does the with heavy lifting for you.


You can update the config file to include your id and secret, and it will take it from there.

  • Tempting as using the fuel sdk instead would be, for internal reasons, sticking to REST is preferred. – Joshua Petersen Feb 3 '15 at 20:24

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.