从Guzzle Oauth身份验证中获取Acess令牌



我目前正在尝试使用guzzle开发与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");
        }
    }

这是我的身份验证流。我有要提出guzzle请求的runcurl方法:

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 GuzzleHttpClient();    
    $response = $client->request($requestType, $url, $options);
    $body = $response->getBody();
    return $body;
}

问题位于这里,getBody()方法返回流,如果我使用getbody() -> getContents(),我会得到一个字符串,没有一个可以帮助我。

关于如何获得访问令牌以便我可以完成身份验证过程的任何想法?

要回答问题本身 - 您只需要将$body施放到字符串即可。它应该是JSON,因此您还需要将其用作上述代码中的对象。因此,您需要做:

而不是runCurl中的return $body;
return json_decode((string)$body);

我建议使用官方客户。问题中的代码存在一些无关的问题,这将使维护变得昂贵。

最新更新