6

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 see 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?

6
  • 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, 2015 at 4:51
  • @JoshuaPeterson did you ever figure this out? I'm having the same problem May 2, 2019 at 21:29
  • @garek007 Unfortunately, no.
    – lilHar
    May 2, 2019 at 22:41
  • are you using guzzle? May 3, 2019 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.
    – lilHar
    May 3, 2019 at 16:23

1 Answer 1

1

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

https://github.com/ExactTarget/FuelSDK-PHP

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

1
  • Tempting as using the fuel sdk instead would be, for internal reasons, sticking to REST is preferred.
    – lilHar
    Feb 3, 2015 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .