Search code examples
phpwordpresssessionhybridauth

HyBirdAuth Twitch Subscriber?


I've written my own function to check if a the user is subscribed, But i did this using my own authentication method, How can i use hybirdauth to use my function to check if the login in user is subscriber? I know i can get the access token by doing Hybrid_Provider_Adapter::getAccessToken(). My function returns a simple httpd 200 if subscriber, any other value isn't important. My main question is where can i insert my function, where do i call to check for the http code. I have add user_subscribe as an additional scope which works.

    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;
}

Solution

    1. Change the public scope to

      public $scope = "user_read user_subscriptions";
      

    2.Change the getUserProfile to below, this will check if user is a sub, if user isnt a sub it will stop the script and redirect page.

        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;
        }