How to access Github landing

Keywords: github JSON Laravel PHP

The Benefits of Accessing github

  1. One-click login, user experience is good (for programmers only)
  2. Application is simple, if it is to apply for QQ landing or micro-blog landing (a meal of verification is as fierce as tiger, ID card photography, phone calls... all kinds of)
  3. Easy to use and easy to learn

Log on to github and register an oauth

  1. Find settings


    settings
  2. developer settings


    developer settings
  3. new oauth app


    image.png
  4. Get client_id and client_secret to fill in application information

    client_id client_secret

  5. Perfect information
    Note the application name and calback URL options, which may be used in subsequent requests

image.png

Official documents

https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps

Development access

  • Accessing github third login is divided into the following steps
    1. Import users into github login authorization page
    2. (If the user agrees to authorize) => GitHub returns to the link filled in by the callback URL when creating the OAuth app and carries a code parameter
    3. Use this code parameter plus your client_id client_secret to get access_token
    4. Use access_token to fetch the interface provided by github to get user information

Code

/**
 * Sending Request Method
 *
 * @param string $url      Request address
 * @param array $data      Request data
 * @param array $headers   Request header
 * @return string|array
 */
function sendRequest($url, $data = [], $headers = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $response = curl_exec($ch) ? curl_multi_getcontent($ch) : '';
    curl_close($ch);
    return $response;
}

// If the user agrees to log in, github returns to callback.php with a code parameter
// At this point, you just need to use this code to get access_token, and then use access_token to get user information.
$url        = "https://github.com/login/oauth/access_token";
$app_id     = "your github oauth app client_id";
$app_secret = "your github oauth app client_secret";

// Combination request parameters
$code   = $_GET['code'];
$params = [
    'client_id'     => $app_id,
    'client_secret' => $app_secret,
    'code'          => $code,
];

// Send the request and get the response information
$response = sendRequest($url, $params, ['Accept: application/json']);
$response = json_decode($response, true);

// If there is a response information, the request is successful
if (!empty($response['access_token'])) {
    // The request succeeded and user information was obtained using access_token
    $access_token = $response['access_token'];
    $url = "https://api.github.com/user";

    // Send a request and call the github API to get user information
    $userInfo =  sendRequest($url,[],[
        "Accept: application/json",
        "User-Agent: ilearn",  // Here (ilearn) is to fill in the application name or github user name
        "Authorization:token {$access_token}"
    ]);

    exit($userInfo);
}

// Print the error message if the login fails
echo "<p>Landing failed</p></pre>";
var_dump($response);
exit("</pre>");

If you don't want to write by yourself, you can also use the laravel.

https://github.com/overtrue/laravel-socialite

Last

If you really want to learn how to access it, try tapping the code yourself. Others are always someone else's.
It's a shallow thing on paper, and you never know it's a matter of courtesy.

Posted by koglakci on Sat, 20 Apr 2019 14:36:33 -0700