I was able to list events from default google calendar, however when trying to list events from other calendars, I got error:
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}
I don't know if external calendars aren't supported, or I got the wrong ID of these external cals. I use this method CalendarList: list to query for all available calendars from my account, but only the first one works (my default). I'm not sure if this is the right way to get the ID:
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary();
echo "\r\n";
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
I've created a test scenario : CalendarList: get of a email without properly sharing the calendar to the one authorized to call the function.
Then, I shared the calendar with the user making the API request:
Kindly take note that :
If you are geting the list of events, you should try and use Events: list.
Hope this information helps.
Related
I am using google API php client and getting below error. This code is working fine before a week. Current its not working and passing error. may be some deprecated by google after that its not working. Please help me out as soon as possible because its affect on live product and can't able to show review.
$http = new GuzzleHttp\Client([
'verify' => false
]);
$client = new Google_Client();
$client->setHttpClient($http);
$client->setApplicationName('Magic Minds WEB');
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setRedirectUri(redirectUri);
$client->setScopes("https://www.googleapis.com/auth/business.manage");
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
$mybusinessService = new Google_Service_MyBusiness($client);
$credentialsPath = tokenJson;
// Load previously authorized credentials from a file.
$accessToken = (array)json_decode(file_get_contents($credentialsPath));
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
// For testing purposes, selects the very first account in the accounts array
$accounts = $mybusinessService->accounts;
// echo "<pre>";
//print_r($accounts);
$accountsList = $accounts->listAccounts()->getAccounts();
print_r($accountsList);
$account = $accountsList[2];
// For testing purposes, selects the very first location in the locations array
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
$location = $locationsList[0];
// Lists all reviews for the specified location
$reviews = $mybusinessService->accounts_locations_reviews;
$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);
$reviewsList = $listReviewsResponse->getReviews();
Getting below error
Fatal error: Uncaught Google\Service\Exception: { "error": { "code": 400, "message": "Request contains an invalid argument.", "errors": [ { "message": "Request contains an invalid argument.", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT", "details": [ { "#type": "type.googleapis.com/google.mybusiness.v4.ValidationError", "errorDetails": [ { "message": "This API will soon be deprecated. Please migrate all the usages to My Business Account Management API - https://developers.google.com/my-business/reference/accountmanagement/rest" } ] } ] } } in /var/www/html/magicmind/magicmindsweb/backend/vendor/google/apiclient/src/Http/REST.php:128 Stack trace: #0 /var/www/html/magicmind/magicmindsweb/backend/vendor/google/apiclient/src/Http/REST.php(103): Google\Http\REST::decodeHttpResponse() #1 [internal function]:
efforts will be appreciated. Thanks in advance
Google is shutting down that api as stated in the error message. They have stated
Starting April 30, 2022, the following four API methods will return errors with increasing frequency, ramping up to 100% shut down within 30 days.
As today is June 07, 2022 You are past the 30 day grace period. So the error message you are getting is the result of that.
This API will soon be deprecated. Please migrate all the usages to My Business Account Management API - https://developers.google.com/my-business/reference/accountmanagement/rest"
I would just start to migrate to the new api as they have directed, This is not something you can fix as that API no longer exists.
See accounts management api
I am creating a web site to interact with Google Calendars and watching resources and I want to stop them, but I can't seem to do that, so Google sends the headers "X-Goog-Channel-Id" and "X-Goog-Resource-Id" with the webhook request which from the documentation seems like that's all that's needed to send back to stop them, but I just keep getting a:
Google\Service\Exception: {
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Channel '0PAA4Z9RXJYMA7YMAV6O' not found for project '309331158475'"
}
],
"code": 404,
"message": "Channel '0PAA4Z9RXJYMA7YMAV6O' not found for project '309331158475'"
}
}
But they should be found as that's what Google has just sent in the header of the webhook. What am I doing wrong?
$headers = getallheaders();
try{
$client = new Google_Client();
$client->setAccessToken(get_google_accesstoken());
$service = new Google_Service_Calendar($client);
$channel = new Google_Service_Calendar_Channel($service);
$channel->setId($headers['X-Goog-Channel-Id']);
$channel->setResourceId($headers['X-Goog-Resource-Id']);
$service->channels->stop($channel);
}catch(Exception $e){
echo $e->getMessage();
}
So the steps I have currently are registering the watch event for the calendar, all good here. Then when the calendar changes Google loads the URL /webhook/google/ on my site and just for concept on that page I have the code above to stop the webhook from happening again, but it shows the error.
I'm generating the watch event with the code below if that helps
$expire = time()+86400;
try {
$client = new Google_Client();
$client->setAccessToken(get_google_accesstoken());
$service = new Google_Service_Calendar($client);
$channel = new Google_Service_Calendar_Channel($client);
$channel->setId(generaterandomstring(20));
$optParams = array('ttl' => $expire);
$channel->setParams($optParams);
$channel->setType('web_hook');
$channel->setAddress($site_url.'/webhook/google/');
$watchEvent = $service->events->watch('email#mysite.com', $channel);
}catch(Exception $e) {
}
I'd guess it's because the channel has already expired.
The $expire = time()+86400 line makes it seem like you're making it expire in 86.4 seconds. Could it be that you're trying to stop the channel watch more than 86 seconds after it was created?
I am always getting this error if I try to get all videos of a channel.
{ "error": { "errors": [ { "domain": "usageLimits", "reason": "keyInvalid", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } }
My code is the following
$api_key = "AIzaSyD...nR8";
stream_context_set_default(['http' => ['ignore_errors' => true]]);
$source_videos = file_get_contents("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={UCuB3qjWes1E8t4yUmbP360Q}&maxResults=1&key={".$api_key."}");
echo $api_key;
echo $source_videos;
I created a project on https://console.developers.google.com/apis/dashboard and added, activated the "YouTube Data API v3", created credentials with "Webserver(eg. Node.js, Tomcat)" as platform and finally copied the key in my php document. I don't know why I can't get it to work. I appreciate help. :)
EDIT:
use Webbrowser(JavaScript) as platform and it should work.
try this code
$API_key = 'AIzaSyCTPwxSs1id2kdv_fupUfbYkfa6Fucp_6A';
$channelID = 'UCTCU28hEulL2jkafQ21uuWA';
$maxResults = 10;
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
note: you need a virtual server to run php (wamp/mamp/xampp)
Use Webbrowser(JavaScript) as platform and it should work.
Am trying to use the official Gmail PHP Library to create a new label. I am using standard Oauth to authenticate the user (all necessary permissions have been given).
But I receive the following error:
<b>Fatal error</b>: Uncaught exception 'Google_Service_Exception' with message '{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "Filter doesn't have any actions"
}
],
"code": 400,
"message": "Filter doesn't have any actions"
}
}
The code is as follows:
$gmail = new Google_Service_Gmail($google);
$label = new Google_Service_Gmail_Label();
$label->setId('Label_8');
$label2 = new Google_Service_Gmail_Label();
$label2->setId('UNREAD');
$label3 = new Google_Service_Gmail_Label();
$label3->setId('INBOX');
$criteria = new Google_Service_Gmail_FilterCriteria();
$criteria->setFrom('test#gmail.com');
$action = new Google_Service_Gmail_FilterAction();
$action->setAddLabelIds(array($label));
$action->setRemoveLabelIds(array($label2,$label3));
$filter = new Google_Service_Gmail_Filter();
$filter->setCriteria($criteria);
$filter->setAction($action);
$result = $gmail->users_settings_filters->create('me',$filter);
Scopes being set:
$google->setScopes(array('https://www.googleapis.com/auth/pubsub','https://mail.google.com','https://www.googleapis.com/auth/gmail.settings.basic'));
Everything looks fine programmatically. I even checked the code and it is assigning the actions. I believe there maybe something wrong with the library. Any advice will help.
Found the issue by analyzing the logs:
$action->setAddLabelIds(array($label));
$action->setRemoveLabelIds(array($label2,$label3));
Should be:
$action->setAddLabelIds(array('Label_8'));
$action->setRemoveLabelIds(array('UNREAD','INBOX'));
Inconsistent API at the moment.
I'm trying to set publish permissions for gmail to a pubsub topic in google cloud.
The application where I implemented this code is running in AWS.
It's a PHP application and I'm using version 2.0.0-RC7 of the google PHP api client.
In code, I implemented the flow as described in the documentation:
Create a topic (Works)
Create a subscription (works)
Grant publish rights to gmail (here I get stuck)
The first two actions are done with the same google client instance, that is authenticated with the service account credentials.
The code:
$scopes = [
'https://www.googleapis.com/auth/pubsub',
'https://mail.google.com',
];
$pushEndpoint = 'https://some.url/google_notifications/';
$client = new Google_Client();
$client->setScopes($scopes);
$client->setAuthConfig($serviceAccountInfo);
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
$service = new Google_Service_Pubsub($client);
// This part works
$topicObject = new Google_Service_Pubsub_Topic();
$topicObject->setName($this->getTopicName());
$service->projects_topics->create($this->getTopicName(), $topic);
// This part also works
$push = new Google_Service_Pubsub_PushConfig();
$push->setPushEndpoint($pushEndpoint);
$subscription = new Google_Service_Pubsub_Subscription();
$subscription->setName($this->getSubscriptionName());
$subscription->setTopic($this->getTopicName());
$subscription->setPushConfig($push);
$service->projects_subscriptions->create($this->getSubscriptionName(), $subscription);
// This part gives the error
$binding = new Google_Service_Pubsub_Binding();
$binding->setRole('roles/pubsub.publisher');
$binding->setMembers(['serviceAccount:gmail-api-push#system.gserviceaccount.com']);
$policy = new Google_Service_Pubsub_Policy();
$policy->setBindings([$binding]);
$setRequest = new Google_Service_Pubsub_SetIamPolicyRequest();
$setRequest->setPolicy($policy);
try {
$result = $service->projects_topics->setIamPolicy($this->getTopicName(), $setRequest);
var_dump($result);
} catch (\Exception $e) {
echo $e->getMessage();
}
The result is always:
{
"error": {
"code": 403,
"message": "User not authorized to perform this action.",
"errors": [
{
"message": "User not authorized to perform this action.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
Can someone tell me what I'm doing wrong ?
It's really annoying to set those permissions by hand all the time.
Thanks.
To use projects.topics.setIamPolicy method your service account must have a "Pub/Sub Admin" role. You can change a role of the account by visiting "IAM & admin" page for your project: https://console.cloud.google.com/iam-admin/iam