PHP SDK, Get Token


(Chester Lee) #1

Trying to use the 2.0 SDK for PHP with Dwolla (Swagger).

And I am trying to get an application token.

The Docs are no help since they simply say:
No support for this language yet. We recommend using an external REST client for making OAuth requests.

So, great.

I have tried the following:

$client_id = ‘myclientid’;
$client_secret = ‘mysecret’;
$basic_credentials = base64_encode($client_id.’:’.$client_secret);
$ch = curl_init(‘https://sandbox.dwolla.com/oauth/v2/token’);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$basic_credentials, ‘Content-Type: application/x-www-form-urlencoded;charset=UTF-8’));
curl_setopt($ch, CURLOPT_POSTFIELDS, ‘grant_type=client_credentials’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = json_decode(curl_exec($ch));
echo $data->access_token;
curl_close($ch);

And this returns a token. But that token does not work. It gives an ID of null in the SDK when making a call.
Specifically:
Uncaught InvalidArgumentException: Missing the required parameter $id when calling id in \vendor\dwolla\dwollaswagger\lib\AccountsApi.php:72

If I use a token generated in the Sandbox, then it DOES work (calling Root).

Many other people seem to have posted this issue, with no response:


I have studied this page:
https://developers.dwolla.com/guides/auth/client-credentials-flow.html

And I can’t find what I am doing wrong. If anyone has a working example of code, or can lend any assistance I would be grateful.


(Spencer Hunter) #2

Hi @CLEE, We do have plans to update the existing php swagger client to support making auth related requests to the API. In the meantime, you’ll want to use an external HTTP client for making this specific request. I’d highly recommend taking a look at Guzzle for doing this as it makes it very easy to send HTTP requests. It’ll look something like the following:

<?php
require('autoload.php');

use GuzzleHttp\Client;

$client = new GuzzleHttp\Client(['base_uri' => 'https://accounts-sandbox.dwolla.com']);

// make a request to the token url
$response = $client->request('POST', '/token', [
    'auth' => [
        'CLIENTID',
        'CLIENTSECRET'
    ],
    'form_params' => [
      'grant_type' => 'client_credentials'
    ]
]);

$responseBody = $response->getBody(true);
$responseArr = json_decode($responseBody, true);
$accessToken = $responseArr['access_token'];
?>

(Chester Lee) #3

Hi Spencer,

Thank you so much for the reply. I do appreciate it.

However, I still seem to have the same issue (whether I use Guzzle, or My code above).

The token that is returned, does not allow me the same API access as when I create the token in Sandbox.

The token created in Sandbox allows me Root access to create customer, check transfers, init transfers etc.

The token created by either Guzzle or my code only allows me access to Events and Webhook subscriptions.

What exactly is the workflow for generating a token that gives root access?