HyBirdAuth Twitch订阅者?

时间:2015-12-04 00:32:37

标签: php wordpress session hybridauth

我已经编写了自己的函数来检查用户是否订阅了,但是我使用自己的身份验证方法做了这个,我如何使用hybirdauth来使用我的函数检查用户登录是否是订阅者?我知道我可以通过Hybrid_Provider_Adapter :: getAccessToken()来获取访问令牌。如果订阅者,我的函数返回一个简单的httpd 200,任何其他值都不重要。我的主要问题是我在哪里可以插入我的功能,我在哪里打电话来检查http代码。我已将user_subscribe添加为可用的附加范围。

    public function subcheck($access_token){
    $username = $this->authenticated_user($access_token);
    $url="https://api.twitch.tv/kraken/users/" . $username . "/subscriptions/".$channel;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.twitchtv.v3+json', 'Authorization: OAuth '.$access_token));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 0);

    // Execute
    $result=curl_exec($ch);
    $httpdStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // Will dump a beauty json :3
    //var_dump(json_decode($result, true));
    return $httpdStatus;
}

1 个答案:

答案 0 :(得分:0)

  1. 将公共范围更改为

    public $scope = "user_read user_subscriptions";
    
  2. 2.将getUserProfile更改为下面,这将检查用户是否为sub,如果用户不是sub,则会停止脚本并重定向页面。

        function getUserProfile()
    {
    
        $data = $this->api->api( "user" );
    
        if ( ! isset( $data->name ) ){
            throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 );
        }
    
        $access_token = $this->api->access_token;
        $username = $data->display_name;
        $channel= "Twitch_Channel_Here"
        $url="https://api.twitch.tv/kraken/users/" . $username . "/subscriptions/".$channel."";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/vnd.twitchtv.v3+json', 'Authorization: OAuth '.$access_token));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST, 0);
        // Execute
        $result=curl_exec($ch);
        $httpdStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        //Check if Sub?? 200 == Sub?
        if ($httpdStatus == "200") {
                    //Do Nothing
                    echo 'SUB';
                }
                else {
            //if not a sub then well... ...
            header("Location: http://google.com/nonsub.jpg"); /* Redirect browser */
            exit();
        }
        $this->user->profile->identifier    = $data->_id;
        $this->user->profile->displayName   = $data->display_name;
        $this->user->profile->photoURL      = $data->logo;
        $this->user->profile->profileURL    = "http://www.twitch.tv/" . $data->name;
        $this->user->profile->email         = $data->email;
    
        if( ! $this->user->profile->displayName ){
            $this->user->profile->displayName = $data->name;
        }