从Guzzle Oauth身份验证获取访问令牌

时间:2017-02-15 06:57:17

标签: php curl guzzle reddit

我正在尝试使用Guzzle通过Oauth开发与Reddit api的连接。我到达了我在Reddit中进行身份验证的地步,然后我获得了授权令牌,但我无法从Guzzle响应中获取访问令牌,因此我可以将其设置为cookie并在后续请求中使用。我目前的代码如下:

public function __construct(){

    if(isset($_COOKIE['reddit_token'])){
        $token_info = explode(":", $_COOKIE['reddit_token']); 
        $this->token_type = $token_info[0];
        $this->access_token = $token_info[1];
    } else { 
        if (isset($_GET['code'])){
            //capture code from auth
            $code = $_GET["code"];

            //construct POST object for access token fetch request
            $postvals = sprintf("code=%s&redirect_uri=%s&grant_type=authorization_code",
                                $code,
                                redditConfig::$ENDPOINT_OAUTH_REDIRECT);

            //get JSON access token object (with refresh_token parameter)

            $token = self::runCurl(redditConfig::$ENDPOINT_OAUTH_TOKEN, $postvals, null, true);

            //store token and type
            if (isset($token->access_token)){
                $this->access_token = $token->access_token;
                $this->token_type = $token->token_type;

                //set token cookie for later use
                $cookie_time = 60 * 59 + time();  //seconds * minutes = 59 minutes (token expires in 1hr) 
                setcookie('reddit_token', "{$this->token_type}:{$this->access_token}", $cookie_time); 
            }
        } else {
            $state = rand();
            $urlAuth = sprintf("%s?response_type=code&client_id=%s&redirect_uri=%s&scope=%s&state=%s",
                               redditConfig::$ENDPOINT_OAUTH_AUTHORIZE,
                               redditConfig::$CLIENT_ID,
                               redditConfig::$ENDPOINT_OAUTH_REDIRECT,
                               redditConfig::$SCOPES,
                               $state);

            //forward user to PayPal auth page
            header("Location: $urlAuth");
        }
    }

这是我的身份验证流程。我有runCurl方法,它会发出guzzle请求:

private function runCurl($url, $postVals = null, $headers = null, $auth = false){

    $options = array(
        'timeout' => 10,
        'verify' => false,
        'headers' => ['User-Agent' => 'testing/1.0']
    );

    $requestType = 'GET';

    if ($postVals != null){
        $options['body'] = $postVals;
        $requestType = "POST";
    }

    if ($this->auth_mode == 'oauth'){
        $options['headers'] = [
            'User-Agent' => 'testing/1.0',
            'Authorization' => "{$this->token_type} {$this->access_token}"];

    }

    if ($auth){
        $options['auth'] = [redditConfig::$CLIENT_ID, redditConfig::$CLIENT_SECRET];

    }

    $client = new \GuzzleHttp\Client();    
    $response = $client->request($requestType, $url, $options);
    $body = $response->getBody();

    return $body;
}

问题在于此处,getBody()方法返回一个流,如果我使用getBody() - > getContents()我得到一个字符串,其中没有一个可以帮助我。

如何获取访问令牌,以便完成身份验证过程?

1 个答案:

答案 0 :(得分:3)

要回答问题本身 - 您只需要将$body转换为字符串。它应该是一个json,所以你还需要json_decode将它用作上面代码中的对象。因此,您需要执行以下操作return $body;而不是runCurl

return json_decode((string)$body);

我建议使用官方客户端。问题中的代码有一些不相关的问题,这将使维护成本很高。