I need help on custom scheme on Google.
I successfully get the user list from delegation.
Below is my code.
$client = new \Google_Client();
$client->setApplicationName('xx');
$scopes = array('www.googleapis.com/auth/admin.directory.user','www.googleapis.com/auth/admin.directory.userschema');
$client->setAuthConfig('C:\Users\xx\xx\public\client_secret.json');
$client->setScopes($scopes);
$user_to_impersonate = 'xx.sg';
$client->setSubject($user_to_impersonate);
$dir = new \Google_Service_Directory($client);
$r = $dir->users->get('xxx#xx.com');
dd($r);
Userscheme have been added to Google also.
But the custom scheme I got is empty.
https://i.stack.imgur.com/0JcfZ.png
If I use projection = full in developers.google.com/admin it works.
https://i.stack.imgur.com/9s3MQ.png
Can someone help?
The documentation says:
You can fetch custom fields in a user profile by setting the projection parameter in a users.get or users.list request to custom or full.
so I would replace your line:
$r = $dir->users->get('xxx#xx.sg');
with:
$optParams = array('projection' => 'full');
$r = $dir->users->get('xxx#xx.sg', $optParams);
Related
I am working on an app that reads and updates values in a Google Spreadsheet using Google Sheets API. I am able to read using my developer key, however attempting to write returns this error:
"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential."
Read (works fine):
$client = new Google_Client();
$client->setApplicationName("XXX");
$client->setDeveloperKey("XXX");
$service = new Google_Service_Sheets($client);
$spreadsheetId = "XXX";
$range = 'promocodes';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
Write code (error):
$client = new Google_Client();
$client->setApplicationName("XXX");
$client->setDeveloperKey("XXX");
$service = new Google_Service_Sheets($client);
$spreadsheetId = "XXX";
$range = 'promocodes!C4';
$values = [1];
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->update($spreadsheetId, $range,
$body, $params);
printf("Cells updated.", $result->getUpdatedCells());
As I understand it, the Google API will allow you to read without an access token (using a developer key for credentials) however you can not update or add information without an oauth2 authentication method which involves sending credentials to google, receiving back a code from them, using that code to get an access token, then using that access token as your credentials to add or update information.
With the following code using Google's PHP API Client I am receiving this response.
Google_Service_Exception with message 'Error calling POST https://www.googleapis.com/dns/v1/projects/PROJECT-NAME/managedZones/DNSZONE/changes: (400) The 'entity.change' parameter is required but was missing.
Where PROJECT-NAME and DNSZONE are my project and zone.
$client_email = MYCLIENT;
$private_key = file_get_contents('config/credentials/KEY.p12');
$scopes = array('https://www.googleapis.com/auth/ndev.clouddns.readwrite');
$project = "PROJECT-NAME";
$managedZone = "DNSZONE";
$creds = new Google_Auth_AssertionCredentials($client_email,$scopes,$private_key);
$client = new Google_Client();
$client->setAssertionCredentials($creds);
$resource = new Google_Service_Dns_ResourceRecordSet();
$resource->kind = "dns#resourceRecordSet";
$resource->name = "testing.DNSZONE.net.";
$resource->rrdatas[] = "testing.otherhost.com.";
$resource->ttl = 800;
$resource->type = "CNAME";
$dns = new Google_Service_Dns($client);
$change = new Google_Service_Dns_Change();
$change->kind = "dns#change";
$change->setAdditions($resource);
$dns->changes->create($project,$managedZone,$change);
I am a bit confused as to how to set this parameter. Or where I am even am to define it.
Just for clarify what the answer is, setAdditions expects an array.
$change->setAdditions([ $resource ]);
I've downloaded the PHP client library for Google Adwords API. I need to fetch 'ConversionsManyPerClick' data from the api, I can't find an option for the same from the client library. But the same time i am able to take this data as file by using this 'AD_PERFORMANCE_REPORT' method. Please help me.
function DownloadCriteriaReportExample(AdWordsUser $user, $filePath) {
// Load the service, so that the required classes are available.
$user->LoadService('ReportDefinitionService');
// Create selector.
$selector = new Selector();
$selector->fields = array('Headline','Description1','Description2','DisplayUrl','AdGroupName','CampaignName','Clicks','ConversionsManyPerClick');
$selector->predicates[] = new Predicate('Status', 'NOT_IN', array('PAUSED'));
$reportDefinition = new ReportDefinition();
$reportDefinition->selector = $selector;
$reportDefinition->reportName = 'ad performance report #' . uniqid();
$reportDefinition->dateRangeType = 'LAST_30_DAYS';
$reportDefinition->reportType = 'AD_PERFORMANCE_REPORT';
$reportDefinition->downloadFormat = 'CSV';
$reportDefinition->includeZeroImpressions = FALSE;
$options = array('version' => 'v201502');
ReportUtils::DownloadReport($reportDefinition, $filePath, $user, $options);
}
Thanks in advance.
Please read the following documentation and also try to modify the core plugin
https://developers.google.com/adwords/api/docs/guides/conversion-tracking
I'm trying to use the newest Google PHP API Lib to update users and i can't find any examples on how to use it to update.
The closest example I can find is this Cannot update user information using Google PHP API Client library but it doesn't have enough info to add on to.
Below is what I have and it's copied off of another job that pulls from the users list. But that is scoped to 'https://www.googleapis.com/auth/admin.directory.user.readonly https://www.googleapis.com/auth/admin.directory.group.readonly https://www.googleapis.com/auth/admin.directory.orgunit.readonly'
and doesn't update anything.
I've searched and searched and cannot seem to find an example of PHP updating a user.
session_start();
require 'vendor/autoload.php';
$SCOPE = 'https://www.googleapis.com/auth/admin.directory.user';
$SERVICE_ACCOUNT_EMAIL = 'BLAHBLAH#developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'blahblah.p12';
$client = new Google_Client();
$client->setApplicationName('BLahBlah.apps.googleusercontent.com');
$adminService = new Google_Service_Directory($client);
$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$cred = new Google_Auth_AssertionCredentials($SERVICE_ACCOUNT_EMAIL, array($SCOPE), $key);
$cred->sub = "chriswhittle#lahlah.com";
$client->setAssertionCredentials($cred);
$email = "fred#tom.com";
$requestUrl = "https://www.googleapis.com/admin/directory/v1/users/" . urlencode($email) ;
$requestMethod = 'PUT';
$data = array("title" => "Cool Guy");
$requestHeader = array('Content-Type' => 'application/json', 'Content-Length' => 'CONTENT_LENGTH');
$request = new Google_Http_Request($requestUrl, $requestMethod,$requestHeader,$data);
$httpRequest = $client->getAuth()->authenticatedRequest($request);
The code was fine, I had to go into our google apps domain security settings and grant user edit access. and adjust the body data to match the json... Here is an example.
$data = array("organizations" => array(array("name"=>"My Company","title"=>"Director, Information Technology","primary"=>true,"customType"=>"Other","department"=>"Information Technology")),"name"=>array("familyName"=> "Admin"));
I've got my session with the valid token that i set up this way :
$session_token = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
// Store the session token in our session.
$_SESSION['cal_token'] = $session_token;
Then i want to be able to do this:
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
But using the token. Instead of the authentication with user/pass/service
I already looked at some example of this but i didn't find any way to make it work.
Thank you everyone!
// Retrieve user's list of Google Docs
$client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['cal_token']);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
foreach ($feed->entries as $entry) {
echo "$entry->title\n";
}