2017-02-15 2 views
0

現在、Guautを使用してOauthを通じてReddit APIへの接続を開発しようとしています。私はRedditで認証を受けた時点で認証トークンを取得しますが、Guzzレスポンスからアクセストークンを取得することはできませんので、Cookieとして設定して以降のリクエストで使用することができます。現在のコードは次のようになります。Guzzle Oauth認証からacessトークンを取得する

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メソッドを持っている:問題はここに常駐

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

https://github.com/reddit/reddit/wiki/OAuth2-PHP-例 –

答えて

3

質問自体に答えるには、$bodyを文字列にキャストするだけです。これはjsonでなければならないので、上記のコードでオブジェクトとして使用するにはjson_decodeする必要があります。だからではなく、あなたのrunCurlreturn $body;の、あなたは何をする必要があります。

return json_decode((string)$body); 

私は公式クライアントカントーを使用することをお勧めします。問題のコードには関係のないいくつかの問題があり、維持するのに費用がかかります。

関連する問題