在为Hybridauth提供商(WordPress)设置自定义终结点时遇到问题

时间:2018-07-04 13:09:08

标签: php hybridauth

我正在尝试使用https://public-api.wordpress.com/rest/v1.1/me/sites端点使用Hybridauth(版本3)获取经过身份验证的用户的WordPress博客的列表。弄清楚了,为此,我需要将WordPress的默认authorize_url端点从https://public-api.wordpress.com/oauth2/authenticate更改为https://public-api.wordpress.com/oauth2/authorize

Hybridauth可让您更改端点,如此处的示例所示:https://github.com/hybridauth/hybridauth/blob/master/examples/example_03.php

不幸的是,这导致以下错误:

  

致命错误:未捕获的TypeError:参数1传递给   Hybridauth \ Adapter \ AbstractAdapter :: setApiEndpoints()必须是   Hybridauth \ Data \ Collection的实例,给定数组

代码:

$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);

我在做什么错?我觉得我错过了一件非常明显的事情。

1 个答案:

答案 0 :(得分:2)

端点应为Hybridauth\Data\Collection的实例。

您可以将当前的终结点数组值传递给Hybridauth\Data\Collection构造函数以创建实例,并将其设置为config数组中的终结点键。

<?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();
}