Last active
August 29, 2015 14:05
Mirror API Account Insert Insufficient Permission Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////////////////////// | |
///GET GOOGLE CLIENT FOR USER oAuth | |
/////////////////////////////////////////////////////////////////// | |
function get_google_client(){ | |
global $api_client_id, $api_client_secret, $app_name, $redirect_url; | |
global $service_client_id, $service_email,$key_file_location,$browser_api_key; | |
$client = new Google_Client(); | |
$client->setApplicationName($app_name); // Set your applicatio name | |
$client->setScopes(array( | |
'email', | |
'profile' | |
) | |
); | |
$client->setClientId($api_client_id); // client id from google API Console | |
$client->setClientSecret($api_client_secret); // set the client secret | |
$client->setRedirectUri($redirect_url); | |
$client->setDeveloperKey($browser_api_key); | |
$client-> setApprovalPrompt("auto"); | |
$client -> setAccessType("online"); | |
return $client; | |
} | |
/////////////////////////////////////////////////////////////////////////// | |
/////////GET GOOGLE CLIENT FOR SERVICE | |
///////////////////////////////////////////////////////////////////////////// | |
function get_service_client(){ | |
global $service_client_id, $service_email, $app_name,$key_file_location,$browser_api_key; | |
$client = new Google_Client(); | |
$client->setApplicationName($app_name); // Set your application name | |
$client->setClientId($service_client_id); | |
$client->setDeveloperKey($browser_api_key); | |
$key = file_get_contents($key_file_location); | |
$cred = new Google_Auth_AssertionCredentials( | |
$service_email, | |
array('https://www.googleapis.com/auth/glass.thirdpartyauth'), | |
$key | |
); | |
$client->setAssertionCredentials($cred); | |
return $client; | |
} | |
/////////////////////////////////////////////////////////////////////// | |
//////////INSERT ACCOUNT METHOD | |
///////////////////////////////////////////////////////////////////////////// | |
function insert_account($service,$userToken, $email) | |
{ | |
$accountType='account-name'; | |
$userDataArray= array(); | |
$userData1= new Google_Service_Mirror_UserData(); | |
$userData1->setKey('email'); | |
$userData1->setValue($email); | |
$userDataArray[]=$userData1; | |
$authTokenArray= array(); | |
$authToken1= new Google_Service_Mirror_AuthToken(); | |
$authToken1->setAuthToken('randomtoken'); | |
$authToken1->setType('randomType'); | |
$authTokenArray[]=$authToken1; | |
$postBody = new Google_Service_Mirror_Account(); | |
$postBody->setUserData($userDataArray); | |
$postBody->setAuthTokens($authTokenArray); | |
try { | |
$account = $service->accounts->insert($userToken, $accountType, $email, $postBody); | |
} catch (Exception $e) { | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////// | |
//////////////////Main file | |
///////////////////////////////////////////////////////////////////////////// | |
if(isset($_SESSION['userToken'])){ | |
//FROM MYGLASS | |
$userToken=$_SESSION['userToken']; | |
$service_client= get_service_client(); | |
$mirrorService = new Google_Service_Mirror($service_client); | |
insert_account($mirrorService, $userToken, $email); | |
$_SESSION['userToken']=null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment