Google Search Console API, URL Inspection - php

I want to use the Google Search Console API to check if an url is indexed.
$client = new Google_Client();
$client->setAuthConfig( json_decode( $json, true ));
$scopes = ['https://www.googleapis.com/auth/webmasters','https://www.googleapis.com/auth/webmasters.readonly'];
$client->addScope($scopes);
$searchConsole = new Google_Service_SearchConsole($client);
$urlRequest = new \Google_Service_SearchConsole_InspectUrlIndexRequest();
$urlRequest->setInspectionUrl($url);
$urlRequest->setLanguageCode('fr');
$urlRequest->setSiteUrl('sc-domain:'.$url);
$urlIndex = new \Google_Service_SearchConsole_Resource_UrlInspectionIndex();
The problem is when I try to create an UrlInspectionIndex to make an inspect() with $urlRequest I get an Exception in the constructor of vendor/google/apiclient/src/Service/Resource.php:66
I don't know what those arguments are. Hope anyone would help me, thank you.

I got it to work like this:
$service = new Google_Service_SearchConsole($client);
$query = new Google_Service_SearchConsole_InspectUrlIndexRequest();
$query->setSiteUrl("https://$domain/");
$query->setInspectionUrl("https://$domain/$page");
$results = $service->urlInspection_index->inspect($query);

Related

Insert Task in Google Task API , Error: 403, message: Request had insufficient authentication scopes

I am trying to insert a new task into TaskList.
here is my code:
$client = getClient();
$service = new Google_Service_Tasks($client);
$optParams = array('maxResults' => 10);
$serviceTasklist = $service->tasklists->listTasklists($optParams);
$serviceTask = $service->tasks->listTasks("#default", $optParams);
try {
$task = new Google_Service_Tasks_Task();
$task->setTitle("here is new task");
$task->setNotes("this is note of new task");
$result = $service->tasks->insert("#default", $task);
return $result->getId();
} catch (Google_Exception $ggex) {
echo "\n Error: " . $ggex->getMessage();
}
I had tried to change scope from Google_Service_Task::TASK_READONLY to Google_Service_Task::TASK, included google task document also do a samething, but I tried then it not work . Can someone give my suggest or tell me know where am I wrong? Thank You !.
For documentation purpose:
The problem was in the token file itself. When you change the scopes you need to make sure to get a new updated token. If you still use the previous token is like haven't changed your code at all.

How to use Google MyBusiness API in php

I am using Google MyBsiness API for fetching all business reviews.
But I am unable to familer with PHP syntax and GET, POST method use in MYBusiness.
After Oath here are code i am using to fetch review
$mybusinessService = new Google_Service_Mybusiness($client);
$accessToken = file_get_contents($credentialsPath);
$reviews = $mybusinessService->accounts_locations_reviews;
echo '<pre>';print_r($reviews->get('ArtechDev'));exit;
But i am getting error 404 (Fatal error: Uncaught exception 'Google_Service_Exception' with message)
I am sure that I don't know how to pass param and which things needed for it. I am logged in as account which having Location 'ArtechDev' also please
let me know that where can i call
https://mybusiness.googleapis.com/v3/accounts/account_name/locations/location_name/reviews
Thanks
I hope you had found the answer to your question a long time ago, anyway I'll leave this hoping help someone else.
/*$accounts previusly populate*/
/*(GMB - v4)*/
$credentials_f = "google_my_business_credentials_file.json";
$client = new Google_Client();
$client->setApplicationName($aplicattion_name);
$client->setDeveloperKey($developer_key);
$client->setAuthConfig($credentials_f);
$client->setScopes("https://www.googleapis.com/auth/plus.business.manage");
$client->setSubject($accounts->email);
$token = $client->refreshToken($accounts->refresh_token);
$client->authorize();
$locationName = "accounts/#######/locations/########";
$mybusinessService = new Google_Service_Mybusiness($client);
$reviews = $mybusinessService->accounts_locations_reviews;
do{
$listReviewsResponse = $reviews->listAccountsLocationsReviews($locationName, array('pageSize' => 100,
'pageToken' => $listReviewsResponse->nextPageToken));
$reviewsList = $listReviewsResponse->getReviews();
foreach ($reviewsList as $index => $review) {
/*Accesing $review Object
* $review->createTime;
* $review->updateTime;
* $review->starRating;
* $review->reviewer->displayName;
* $review->reviewReply->comment;
* $review->getReviewReply()->getComment();
* $review->getReviewReply()->getUpdateTime();
*/
}
}while($listReviewsResponse->nextPageToken);
$acounts comes from:
$mybusinessService = new Google_Service_MyBusiness(...);
$accounts = $mybusinessService->accounts;

Why do I get 404 Error when using Google Classroom API?

I am writing an app in PHP which will connect to my domains's Google Classroom. However I get the following error when I try to do anything with the Google Classroom API:
Message: Error calling GET https://www.googleapis.com/v1/courses?pageSize=100: (404) Not Found
My code so far:
$scopes = array(
'https://www.googleapis.com/auth/classroom.courses',
'https://www.googleapis.com/auth/classroom.courses.readonly',
'https://www.googleapis.com/auth/classroom.rosters',
'https://www.googleapis.com/auth/classroom.rosters.readonly'
);
$gServiceEmail = "random#developer.gserviceaccount.com";
$gServiceKey = file_get_contents("../path/to/cert.p12");
$client = new Google_Client();
$gAuth = new Google_Auth_AssertionCredentials(
$gServiceEmail,
$scopes,
$gServiceKey
);
$gAuth->sub = "user#mydomain.com";
$client->setAssertionCredentials($gAuth);
$service = new Google_Service_Classroom($client);
$results = $service->courses->listCourses();
I have enabled the scopes in the API Settings in the Google Admin Console for the service account and enabled the api in the developer console. Where am I going wrong?
I think your endpoint is wrong according to the documentation of Classroom API. Try changing it to https://classroom.googleapis.com
sample request:
GET https://classroom.googleapis.com/v1/courses?pageSize=100&key={YOUR_API_KEY}
Course with id is not found.
For a list of courses, use the courses.list(), as shown in the following sample.
$client = getClient();
$service = new Google_Service_Classroom($client);
// Print the first 10 courses the user has access to.
$optParams = array(
'pageSize' => 10
);
$results = $service->courses->listCourses($optParams);
if (count($results->getCourses()) == 0) {
print "No courses found.\n";
} else {
print "Courses:\n";
foreach ($results->getCourses() as $course) {
printf("%s (%s)\n", $course->getName(), $course->getId());
}
}
References:
https://developers.google.com/classroom/quickstart/php?hl=en
https://developers.google.com/classroom/guides/manage-courses?hl=en

Batch request Google Calendar php API

I'm working on a google Calendar sync with my application.
I'm using the latest google-api-php-client
Now I want to update all my event, so i want to use the batch operation.
The example code of the php client api is:
$client = new Google_Client();
$plus = new Google_PlusService($client);
$client->setUseBatch(true);
$batch = new Google_BatchRequest();
$batch->add($plus->people->get(''), 'key1');
$batch->add($plus->people->get('me'), 'key2');
$result = $batch->execute();
So when I "translate" it to the calendar API, I become the following code:
$client = new Google_Client();
$this->service = new Google_CalendarService($client);
$client->setUseBatch(true);
// Make new batch and fill it with 2 events
$batch = new Google_BatchRequest();
$gEvent1 = new Google_event();
$gEvent1->setSummary("Event 1");
$gEvent2 = new Google_event();
$gEvent2->setSummary("Event 2");
$batch->add( $this->service->events->insert('primary', $gEvent1));
$batch->add( $this->service->events->insert('primary', $gEvent2));
$result = $batch->execute();
But when I run this code, I get this error:
Catchable fatal error: Argument 1 passed to Google_BatchRequest::add()
must be an instance of Google_HttpRequest, instance of Google_Event given
And I do not think that "$plus->people->get('')" is a HttpRequest.
Does anybody know what I do wrong, or what method / object I should use to add in the batch?
Or what the correct use of the batch operation for the calendar is?
Thanks in advance!
I had the same problem while working with inserts to the MirrorService api, specifically with timeline items. What is happening is that the the Google_ServiceRequest object is seeing that you've set the useBatch flag on the client and is actually returning returning Google_HttpRequest object before executing the call to Google but the insert statement in the calendar service doesn't properly handle it as such and ends up returning the calendar event object instead.
It also looks like your params to batch->add are backwards. Should be:
$batch->add( $this->service->events->insert($gEvent1, 'primary'));
Here is my modification to the insert method (you'll need to do this in the calendar service with the proper object input to the method). Just a few lines to make it check what class is coming back from the ServiceRequest class:
public function insert(google_TimelineItem $postBody, $optParams = array()) {
$params = array('postBody' => $postBody);
$params = array_merge($params, $optParams);
$data = $this->__call('insert', array($params));
if ($this->useObjects()) {
if(get_class($data) == 'Google_HttpRequest'){
return $data;
}else{
return new google_TimelineItem($data);
}
} else {
return $data;
}
}
you can use this code to insert events in batch:
public function addEventInBatch($accessToken, $calendarId, array $events)
{
$client = new Google_Client();
$client->setAccessToken($accessToken);
$client->setUseBatch(true);
$service = new Google_Service_Calendar($client);
$batch = $service->createBatch();
collect($events)->each(fn ($event) => $batch->add($service->events->insert($calendarId, $event)));
return $batch->execute();
}

Issue with sending direct message to someone with API for Twitter

Hey guys I am using the following:
function confirm($x, $y)
{
$inerlevelWatch = $x;
$NewDirectMessage = $y;
$directmessconect = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $currentTwitteroauth, $currentTwitteroauthsecret);
$parameters = array('screen_name' => $inerlevelWatch, 'text' => $NewDirectMessage);
$method = 'direct_messages/new';
$directmessconect->post($method, $parameters);
}
So I give the $innerlevelWatch a screen_name so lets say, Joe and than the new direct message that is passed to $NewDirectMessage = 'testing'. I create new connection just for the sending of the message inside this function and set the parameters. Set the method and then make the post.
The issue is, It does not seem to actually send and I am not sure why, so if you could give me a hand I would appreciate it!
`$consumerKey="your consumerKey";
$consumerSecret ="consumerSecret";
$accessToken ="accessToken ";
$accessTokenSecret ="accessTokenSecret";`
// Create object
`$tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);`
// Set status message
`$tweetMessage="message";`
`$scrren_name='abc';`
`if(strlen($tweetMessage)<=140)
{
$tweet->post('direct_messages/new', array('screen_name' => $scrren_name,'text'=>$tweetMessage));
}`
hope it help you

Categories