phphybridauth

Having trouble setting custom endpoints for Hybridauth provider (WordPress)


I'm trying to use Hybridauth (ver 3) to get a list of an authenticated user's WordPress blogs, using the https://public-api.wordpress.com/rest/v1.1/me/sites endpoint. Figured out that in order to do so, I need to change the default authorize_url endpoint for WordPress from https://public-api.wordpress.com/oauth2/authenticate to https://public-api.wordpress.com/oauth2/authorize.

Hybridauth lets you change the endpoint, as in this example here: https://github.com/hybridauth/hybridauth/blob/master/examples/example_03.php

Unfortunately, this results in the following error:

Fatal error: Uncaught TypeError: Argument 1 passed to Hybridauth\Adapter\AbstractAdapter::setApiEndpoints() must be an instance of Hybridauth\Data\Collection, array given

The code:

$config = [
    "callback"      => APP_URL."/callback", 
    "keys"          => array("id" => "XXXXXX", "secret" => "YYYYYYYYYY"),
    "scope"         => array("global", "auth"),
    "endpoints"     => [
        "authorize_url"    => "https://public-api.wordpress.com/oauth2/authorize",
    ]
];

$adapter = new Hybridauth\Provider\WordPress($config);

What am I doing wrong? I feel like I'm missing out on something horribly obvious.


Solution

  • Endpoints should be an instance of Hybridauth\Data\Collection.

    You can pass the current endpoints array value to Hybridauth\Data\Collection constructor to create an instance and set this to the endpoints key in the config array.

    <?php
    
    include './vendor/autoload.php';
    
    
    $endpoints = new Hybridauth\Data\Collection([
        'api_base_url'     => 'https://public-api.wordpress.com/rest/v1.1/',
        'authorize_url'    => 'https://public-api.wordpress.com/oauth2/authorize',
        'access_token_url' => 'https://public-api.wordpress.com/oauth2/token',
    ]);
    
    $config = [
        'callback'  => Hybridauth\HttpClient\Util::getCurrentUrl(),
        'keys'      => [ 'id' => 'client-id', 'secret' => 'client-secret' ],
        'endpoints' => $endpoints
    ];
    
    try {
        $adapter = new Hybridauth\Provider\WordPress( $config );
        $adapter->authenticate();
    
        $tokens = $adapter->getAccessToken();
        print_r($tokens);
    
        $userProfile = $adapter->getUserProfile();
        print_r( $userProfile );
    
        $adapter->disconnect();
    }
    catch (Exception $e) {
        echo $e->getMessage();
    }