Skip to content

Instantly share code, notes, and snippets.

@LubnaEssa
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LubnaEssa/c7c076fc9e440f0cdd52 to your computer and use it in GitHub Desktop.
Save LubnaEssa/c7c076fc9e440f0cdd52 to your computer and use it in GitHub Desktop.
Mirror API Account Insert Insufficient Permission Error
///////////////////////////////////////////////////////////////////
///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