我如何使用Laravel社交名媛检索用户github存储库? [英] How do i retrieve a user github repositories with Laravel socialite?
本文介绍了我如何使用Laravel社交名媛检索用户github存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在使用社交名流已经有一段时间了,我想知道是否有任何内置方法可以让我检索用户存储库.
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.
我熟悉 laravel网站上提供的方法列表. . 知道如何在给定用户令牌的情况下获取回购协议吗?
I'm familiar with the list on method offered on laravel website. Any idea how to fetch the Repos given the user token ?
推荐答案
我找到了另一种方法.在下面找到代码:
I found another to do that . Find the code below :
public function handleProviderCallback()
{
$user = Socialite::driver('github')->user();
$this->getUserRepos($user);
}
Get Repos函数如下:
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 />';
}
}
而且工作迅速.
这篇关于我如何使用Laravel社交名媛检索用户github存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文