How do i retrieve a user github repositories with Laravel socialite?

柔情痞子 提交于 2019-12-11 02:59:48

问题


Been using socialite for quite a while now and i'm wondering if there is any built-in method that allows me to retrieve a user repositories.

I'm familiar with the list on method offered on laravel website. Any idea how to fetch the Repos given the user token ?


回答1:


I found another to do that . Find the code below :

In the callback function i added a function that uses curl to fetch repos

public function handleProviderCallback()
{
    $user = Socialite::driver('github')->user();

    $this->getUserRepos($user);

}

The Get Repos function is as below :

private function getUserRepos($user)
{

 $name=$user->getNickname();
 $token=$user->token;
 // We generate the url for curl
 $curl_url = 'https://api.github.com/users/' .$name. '/repos';

 // We generate the header part for the token
 $curl_token_auth = 'Authorization: token ' . $token;

 // We make the actuall curl initialization
 $ch = curl_init($curl_url);

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 // We set the right headers: any user agent type, and then the custom token header part that we generated
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Awesome-Octocat-App', $curl_token_auth));

 // We execute the curl
 $output = curl_exec($ch);

// And we make sure we close the curl       
curl_close($ch);

 // Then we decode the output and we could do whatever we want with it
 $output = json_decode($output);
 foreach ($output as  $repo) {
 print '<a href="' . $repo->html_url . '">' . $repo->name . '</a><br />';
 }
}

And it works swiftly .



来源:https://stackoverflow.com/questions/51566875/how-do-i-retrieve-a-user-github-repositories-with-laravel-socialite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!