I have a simple user system with basic information: email and password. Instead of using the Google api as a "Sign in" feature:
Is there any way i can "Link" the user's Youtube Channel to their account?
Is there a way i can Link multiple channels to a user's account? If so, how would i keep track of which user to get data from?
I have written a few PHP Classes that take care of Google's OAuth Flow, as well as storing the access token, expiration time, and the refresh token in a database (You may think "Don't reinvent the wheel", but i prefer to learn as well by writing these classes).
As i kept going through my logic and how i would lay everything out, i just did not understand how i Link the user's account to my previous account. EG: When they login, how do i get the user's Linked channels?
Here is some of my code from my Client.php
public function authinticate($authorizationCode)
{
// Now we need to do a POST request to get an Access token and a refresh token
$post = array("grant_type" => "authorization_code", "code" => $authorizationCode, "client_id" => $this->clientID, "client_secret" => $this->clientSecret, "redirect_uri" => $this->redirectUri);
$postText = http_build_query($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::Google_OAuth_Token_Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postText);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
//Decode the returned values
$responseArray = json_decode($result, true);
// Set out values
$this->accessToken = $responseArray['access_token'];
$this->refreshToken = $responseArray['refresh_token'];
$this->expiresIn = $responseArray['expires_in'];
}
And some from my TokenStorage Class:
// Store the token Data response from google, (IF reresh token is passed with a value, this means that the user JUST authorized our application, so we need to store that value)
public function storeTokenData($channelID, $accessToken, $refreshToken, $expirationTime)
{
$firstRequest = "INSERT INTO tokens (channelID, accessToken, refreshToken, expirationTime) VALUES (?, ?, ?, DATE_ADD(NOW(), INTERVAL $expirationTime SECOND)";
$linkedRequest = "INSERT INTO tokens (channelID, accessToken, expirationTime) VALUES (?, ?, DATE_ADD(NOW(), INTERVAL $expirationTime SECOND)";
if($refreshToken == null)
{
if ($storeTokenData = global $mysqli->prepare($firstRequest))
{
$storeTokenData->bind_param('sss', $channelID, $accessToken, $refreshToken);
if($storeTokenData->execute()) {
// Data stored success...
return true;
} else {
// Something happened...
return false;
}
}
}
else
{
if ($storeTokenData = global $mysqli->prepare($linkedRequest))
{
$storeTokenData->bind_param('ss', $channelID, $accessToken);
if($storeTokenData->execute()) {
// Data stored success...
return true;
} else {
// Something happened...
return false;
}
}
}
}