PHP Classes

Version 1.3.0 released

Recommend this page to a friend!

      Github PHP API Library  >  All threads  >  Version 1.3.0 released  >  (Un) Subscribe thread alerts  
Subject:Version 1.3.0 released
Summary:Self service to invite friend to your GitHub Repo
Messages:1
Author:Mat Jung
Date:2020-12-27 22:00:25
 

  1. Version 1.3.0 released   Reply   Report abuse  
Picture of Mat Jung Mat Jung - 2020-12-27 22:00:25
There are plenty of ways to grant a friend write/push permissions to your GitHub repos.

One of them is php based.
Basically the idea is, that you host a webbpage, where your friends may signup for your GitHub repo.

Sample usage:
micro-work.net/ghac/ghacadduser2rep ...

You just need to hit the Sign in with GitHub button and you become a member of matjung/phpclasses_friends

How to do that yourself:
Source code: ghacadduser2repo.php

Requirements:
github.com/settings/developers

The sample implementation is based on both:
one OAuth App
one Personal Access Token

With OAuth you get the GitHub username of your friend.
With Personal Access Token you grant your friend access to your repo.


Step1:
displayInputForm();
/* just display a GitHub Login button */

Step2:
processPost();
/*
require_once("phpGithub.php");
include("authenticate_oauth.inc");
/* client_id client_secret*/
$hub = new php\github\phpGithub();
if(gethostname()==LOCAL_HOST_NAME) { $hub->AddCurlConfig(CURLOPT_CAINFO, LOCAL_CURLOPT_CAINFO);} /* path to php/curl/cacert if needed */

$scope="user"; // mandatory
$github_callback="https://www.your-domain.net/your/path/ghacadduser2repo.php/";
$hub->requestAccessToken($client_id,$scope,$github_callback);
This is where the magic happens
The web browser get redirected to
github.com/login/oauth/authorize?cl ...

*/

Step3:
processGitHub();
/*
main action for obtaining your friends name and inviting him/her to join you

require_once("phpGithub.php");
require_once("phpGithubUser.php");
include("authenticate_oauth.inc");
/* client_id client_secret*/
$hub = new php\github\phpGithub();
if(gethostname()==LOCAL_HOST_NAME) { $hub->AddCurlConfig(CURLOPT_CAINFO, LOCAL_CURLOPT_CAINFO);}
/* path to php/curl/cacert if needed */
$github_callback="https://www.your-domain.net/your/path/ghacadduser2repo.php/";

/* we receive a temp code that we need to convert into a token */ $response=$hub->convertCodeToToken($client_id,$client_secret,$_GET["code"],$_GET["state"],$github_callback);


if($response->err=="401")
{ $hub->requestAccessToken($client_id,$scope,$github_callback);}

if($response->failed)
{ echo "failed";echo $response;}

if($response->success)
{
if(strpos($response->info["content_type"],"application/x-www-form-urlencoded") !== false)
{
parse_str($response->response,$oauth);
$token=$oauth["access_token"];
$Authorization="Authorization: token $token";
$hub->AddCurlConfig(CURLOPT_HTTPGET, true);
$hub->AddCurlHeader(CURLOPT_HTTPHEADER, array($Authorization,"Accept: application/vnd.github.machine-man-preview+json"));
$response=$hub->returnCurrentUser();
/* the user needs to approve the access request */

if($response->success)
{
$user = new php\github\phpGithubUser($response);
inviteToRepository($user,$hub);
} else {
echo "GitHub user rejected authorization.
Or something else happened<br>";
}} else {echo "Unknown Error. Perhaps GitHub v3 experienced a change<br>"; }}
*/

Step 4
function inviteToRepository($user,$hub)

Parameters:
$your_access_token
$github_owner -> you
$github_repo -> your repo


$hub->AddCurlHeader(CURLOPT_HTTPHEADER,
array("Authorization: token $your_access_token","Accept: application/vnd.github.machine-man-preview+json"));
$response=$hub->putRepositoryCollaborator($github_owner,$github_repo,$user->user);
if($response->success)
{
Your friend has been added to the repo
}

}

Hope it works also for you.
FAQ
Can you add your friends to 15 repos at the same time?
Yes, you can