0

First time trying this. In a new Laravel 5.7 + GuzzleHttp project, I am looking to access a Wordpress 5.0.3 site, using JWT Authentication for WP-API plugin for auth.

I have a Wordpress user and password set up, and testing the API url with Postman it works OK.

{
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9....",
    "user_email": "webmaster@wptest.com.au",
    "user_nicename": "aeaz62bxqx",
    "user_display_name": "Aeaz62BXqx"
}

On the Laravel Guzzle side I test the same uri like this:

$base_uri = $request->channel_url . '/wp-json/jwt-auth/v1/';

try {

    $client = new client();
    $session = $client->post($base_uri . 'token', [
        'headers' => [
            'Accept' => 'application/json',
        ],
        'form_params' => [
            'username' => $request->wp_user,
            'password' => $request->wp_password,
        ]
    ]);

    // GET BODY CONTENT HERE
    $body = json_decode($session->getBody()->getContents());

    // LOG HERE
    Log::info(print_r($session, true));   // SEE BELOW
    Log::info(print_r($body));   // EMPTY

    // GRAB TOKEN HERE
    $wp_token = $body->token;


    if ($session->getStatusCode() == '200') {

        $website->update([
            'wp_user' => $request->wp_user,
            'wp_password' => $request->wp_password,
            'wp_token' => $wp_token,
        ]);

    } else {

        return response()->json([
            'message' => 'We cannot log in to your Wordpress site at this time.'
        ], 401);

    }

} catch (RequestException $e) {
    $error = Psr7\str($e->getRequest());
    if ($e->hasResponse()) {
        $error .= '<br/>' . Psr7\str($e->getResponse());
    }
    return response()->json([
        'message' => 'There was an error connecting to Wordpress (U: ' . $request->wp_user . ', P: ' . $request->wp_password . ')'
    ], 401);
}

When I execute the above and log the response, I get a 200 status code and the following which does not seem to contain the token, user_email, etc. If I purposely change the user name or password, I get a 403 forbidden, so I assume it has authenticated when it responds 200.

Any help welcome.

[2019-02-20 11:42:03] local.INFO: 1  
[2019-02-20 11:42:03] local.INFO: GuzzleHttp\Psr7\Response Object
(
    [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
    [statusCode:GuzzleHttp\Psr7\Response:private] => 200
    [headers:GuzzleHttp\Psr7\Response:private] => Array
        (
            [Server] => Array
                (
                    [0] => nginx/1.15.5
                )

            [Date] => Array
                (
                    [0] => Wed, 20 Feb 2019 11:42:03 GMT
                )

            [Content-Type] => Array
                (
                    [0] => application/json; charset=UTF-8
                )

            [Transfer-Encoding] => Array
                (
                    [0] => chunked
                )

            [Connection] => Array
                (
                    [0] => keep-alive
                )

            [X-Robots-Tag] => Array
                (
                    [0] => noindex
                )

            [Link] => Array
                (
                    [0] => <http://wptest.com.au/wp-json/>; rel="https://api.w.org/"
                )

            [X-Content-Type-Options] => Array
                (
                    [0] => nosniff
                )

            [Access-Control-Expose-Headers] => Array
                (
                    [0] => X-WP-Total, X-WP-TotalPages
                )

            [Access-Control-Allow-Headers] => Array
                (
                    [0] => Authorization, Content-Type
                )

            [Allow] => Array
                (
                    [0] => POST
                )

        )

    [headerNames:GuzzleHttp\Psr7\Response:private] => Array
        (
            [server] => Server
            [date] => Date
            [content-type] => Content-Type
            [transfer-encoding] => Transfer-Encoding
            [connection] => Connection
            [x-robots-tag] => X-Robots-Tag
            [link] => Link
            [x-content-type-options] => X-Content-Type-Options
            [access-control-expose-headers] => Access-Control-Expose-Headers
            [access-control-allow-headers] => Access-Control-Allow-Headers
            [allow] => Allow
        )

    [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
    [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
        (
            [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #23
            [size:GuzzleHttp\Psr7\Stream:private] => 
            [seekable:GuzzleHttp\Psr7\Stream:private] => 1
            [readable:GuzzleHttp\Psr7\Stream:private] => 1
            [writable:GuzzleHttp\Psr7\Stream:private] => 1
            [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
            [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                (
                )

        )

)
TheRealPapa
  • 4,393
  • 8
  • 71
  • 155

1 Answers1

0

Well, this worked.

$body_content = json_decode($session->getBody(), true);

$token = $body_content['token'];

I got a better understanding of this from here.

TheRealPapa
  • 4,393
  • 8
  • 71
  • 155
  • Nice, I just wanted to post an answer. `print_r` seems to be the issue. `Log::info(json_decode($session->getBody()->getContents()));` would work as well. – piscator Feb 20 '19 at 13:15