我如何检索 github 社交名流的用户 github 存储库?
How do i retrieve a user github repositories with Laravel socialite?
使用 socialite 已经有一段时间了,我想知道是否有任何内置方法可以让我检索用户存储库。
我熟悉 laravel website 上提供的方法列表。
知道如何在给定用户令牌的情况下获取 Repos 吗?
我找到了另一个来做那个。找到下面的代码:
在回调函数中,我添加了一个使用 curl
获取 repos
的函数
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 />';
}
}
而且效果很快。
使用 socialite 已经有一段时间了,我想知道是否有任何内置方法可以让我检索用户存储库。
我熟悉 laravel website 上提供的方法列表。 知道如何在给定用户令牌的情况下获取 Repos 吗?
我找到了另一个来做那个。找到下面的代码:
在回调函数中,我添加了一个使用 curl
获取 repos
的函数
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 />';
}
}
而且效果很快。