Using Guzzle 6 to consume an API

Hello all

I’m a bit new to API’s, so please excuse me if this is a silly question.

I need to create an API server and client.
On recommendation from a friend, I am using apigility to create the server.

I’m trying to get an access code, and here are the parameters needed.
The page that gives the access code is at: /oauth/receivecode
It takes these variables in the URL: response_type=code&client_id=testclient&redirect_uri=/oauth/receivecode&state=xyz
I also need to post a variable called: authorized with value yes
I also need to set two headers: ‘accept’ => ‘application/json’ , ‘Content-Type’ => ‘application/json’

It seems to be working fine, so now I need to consume it.
On recommendation from another friend, I am using Guzzle 6 for the client side.
Here is the code:

    $s_base_url = 'http://www.apigility.loc';
    $s_client = 'testclient';
    $s_redirect_uri = '/oauth/receivecode';

    $o_client = new Client(['base_uri' => $s_base_url]);

    $s_uri = '/oauth/authorize';
    $s_response_type = 'code';

    $s_get_string = $s_uri . '?response_type=' . $s_response_type . '&client_id=' . $s_client . '&redirect_uri=' . $s_redirect_uri . '&state=xyz';

    $a_header = array('accept' => 'application/json' , 'Content-Type' => 'application/json');

    $a_form_data = array('authorized' => 'yes');

    $a_params = array('headers' => $a_header , 'form_params' => $a_form_data);

try {
    $o_response = $o_client -> post($s_get_string , $a_params);
}
catch (GuzzleHttp\Exception\ClientException $e){
    var_dump($e);
}

This does not work.
It returns a 400 Bad Request, with no real error message, so I’m stumped as to what to do.
I’ve also tried using the multipart option, but I can’t get that to work either.

Can anyone help?

Thanks in advance
Daniel

1 Like

I’d assume the URL you try is incorrect. I’ve done a bit of OAuth in the past and it was never as simple as just appending the oauth path somewhere. (see also https://www.apigility.org/documentation/auth/authentication-oauth2 for the apigility docs on OAuth).

if you’re starting with using a public API, use one without authentication at first.

Your environment might not be set-up to pass the required headers. I’ve had issues in the past before with that locally especially if you’re using fpm.

Hi Dormilich

Thanks for the reply buddy.
It is appreciated.

The URL is correct thought.
It’s on my local machine.
.loc is how I setup my local environment.
Also, if I leave out the form data parameters, everything works, but no code is returned (only the client id that I passed, is returned back).

Have you got any other thoughts?

Hi oddz

Thanks for your reply.
I thought about what you are saying.
When I leave out the form parameters, and only send the headers, the code ‘works’ but no code is returned.
So the headers are definitely going through.
Also, when I leave out the headers, a 406 Bad Request is returned.

What is fpm?

then the question comes up if you set up Apigility correctly.

FPM - FastCGI Process Manager

I believe that I setup my APIgility correctly.
I watched the tutorials and did some reading, and it’s fairly simple to setup.
I added the authentication (oauth).
My oauth2 route is correct.
I believe that my MySQL tables are correct (found the create statements in a tutorial).
The authentication type is set to oauth2 in my API.
At this stage in the code above, I haven’t reached a method, but I checked that too: authorization is setup, headers are setup.
I’ve been through everything on the APIgility side and can’t see anything incorrect.

Not familiar with FMP, so I don’t think that I’m using that.

Thanks for your help so far :smile:
It is appreciated.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.