Not receiving webhook notification from drive, why? - php

Im trying to use push notification from Google Drive SDK for receive notification for all changes into an account, I follow the steps Registering domain, then creating notification channel and it works, I get this object Google_Service_Drive_Channel from google-api-php-client:
$client = new Google_Client();
$client->setApplicationName("APP_NAME");
$service = new Google_Service_Drive($client);
$key = file_get_contents('my_private_key_location.p12');
$cred = new Google_Auth_AssertionCredentials(
'email_address_from_the_client#developer.gserviceaccount.com',
array('https://www.googleapis.com/auth/drive'),
$key
);
$client->setAssertionCredentials($cred);
// Channel
$channel = new Google_Service_Drive_Channel();
$channel->setId(uniqid());
$channel->setAddress('https://my.custom.url');
$channel->setKind('api#channel');
$channel->setType('web_hook');
$test = $service->changes->watch($channel);
print_r($test);
// Response printing the result
Google_Service_Drive_Channel Object
(
[address] =>
[expiration] => 1407171497000
[id] => xxxxxxxxxxxxxxxxxx
[kind] => api#channel
[params] =>
[payload] =>
[resourceId] => xxxxxxxxxxxxxxxxxxxx
[resourceUri] => https://www.googleapis.com/drive/v2/changes?includeDeleted=true&includeSubscribed=true&maxResults=100&alt=json
[token] =>
[type] =>
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
So, I go to the google drive account for move some file, but I still don't receive anything from the webhook inside the location specified "address" parameter.
I receive X-Goog-Resource-State = sync header just when the channel is created, but others events like (add, remove, trash, untrash, change, update) don't.
Any help, Thank's

I am guessing the Drive account you're editing isn't associated with that service account, as I don't see you using the sub parameter anywhere. Can you try it with web based OAuth 2.0 as a test so you can establish it is getting access to the same Drive account as the one you are modifying. Take a look at the Drive docs on Application accounts for more on this: https://developers.google.com/drive/web/service-accounts

This was the solution for my bug Delegate domain-wide authority to your service account

Related

Email verification with Xverify

I am using API of Xverify for email verifiation
Below is my API code:
require 'XverifyClientAPI.php';
$api_key = 'myapikey'; // Your API Key
$options = array();
$options['type'] = 'json'; // API response type
$options['domain'] = 'addeddomainname';// Reruired your domain name
$client = new XverifyClientAPI($api_key,$options);
$data = array();
$data['email'] = 'test#xverify.com';
$client->verify('email',$data);
echo '<pre>';
echo "valid: ", var_dump($client->is_valid()), "\n";
echo "status: ", $client->status(), "\n";
print_r($client->getReponseAsObject());// Convert the json response into object
Here I have use API key which is provided in my xverify account and domain name which is added in my xverify account
but still its give me error like below
valid: bool(false)
status: bad_request
stdClass Object
(
[syntax] => 1
[handle] => test
[domain] => xverify.com
[catch_all] => unknown
[address] => test#xverify.com
[error] => 0
[status] => bad_request
[responsecode] => 504
[message] => Reach the API Limit
[duration] => 0.013515949249268
)
I am not getting what is the problem with this ,can any body help me ?
Make sure you are using the correct url to call the api, in my case i had to the the domian name us.xverify.com to make it work, before i was using only xverify.com which always gave me the result same as yours(api limit reached), to check the correct api call log in to your xverify account and then click on setting and select email and scroll to the bottom of the page and check the correct url. If the id was created in the us i guess you have to use us.xverify.com

Doctrine 2 and OAuth2.0 Server PHP Client Credentials are invalid

I am trying to implement OAuth2 with Doctrine as an entity manager. I followed this tutorial exactly:
http://bshaffer.github.io/oauth2-server-php-docs/cookbook/doctrine2/
Here is my code that is called when a user makes a request to the API:
// obtaining the entity manager
$entityManager = EntityManager::create($conn, $config);
$clientStorage = $entityManager->getRepository('OAuthClient');
$clients = $clientStorage->findAll();
print_r($clients); // We are getting the clients from the database.
$userStorage = $entityManager->getRepository('OAuthUser');
$accessTokenStorage = $entityManager->getRepository('OAuthAccessToken');
$authorizationCodeStorage = $entityManager->getRepository('OAuthAuthorizationCode');
$refreshTokenStorage = $entityManager->getRepository('OAuthRefreshToken');
//Pass the doctrine storage objects to the OAuth2 server class
$server = new \OAuth2\Server([
'client_credentials' => $clientStorage,
'user_credentials' => $userStorage,
'access_token' => $accessTokenStorage,
'authorization_code' => $authorizationCodeStorage,
'refresh_token' => $refreshTokenStorage,
], [
'auth_code_lifetime' => 30,
'refresh_token_lifetime' => 30,
]);
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($clientStorage));
// handle the request
$server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
Whenever a call using the correct credentials is made, I get this response:
Array
(
[0] => OAuthClient Object
(
[id:OAuthClient:private] => 1
[client_identifier:OAuthClient:private] => testclient
[client_secret:OAuthClient:private] => testpass
[redirect_uri:OAuthClient:private] => http://fake.com
[hashOptions:protected] => Array
(
[cost] => 11
)
)
[1] => OAuthClient Object
(
[id:OAuthClient:private] => 2
[client_identifier:OAuthClient:private] => trevor
[client_secret:OAuthClient:private] => hutto
[redirect_uri:OAuthClient:private] => https://www.another.com
[hashOptions:protected] => Array
(
[cost] => 11
)
)
)
{"error":"invalid_client","error_description":"The client credentials are invalid"}
So we are getting the clients from the database, we should be checking them, and returning that they in fact exists and issuing an access token. However, for some reason, OAuth2 Server (can be seen here) can not match the given credentials with the stored credentials.
I do not think this is a Doctrine problem because I can retrieve the results fairly easily using findAll().
My question is:
Why is this happening, and how can I fix it?
I found the problem. In the tutorial (http://bshaffer.github.io/oauth2-server-php-docs/cookbook/doctrine2/) they fail to mention that when the client secret is checked with against a hashed version of the provided client secret.
In the tutorial they do not hash the example client secret when they put it in the database.
If you hash your client secret when inserting it into the database, it will work as expected.

Getting user country Facebook API PHP SDK

Currently having a few issues accessing the country from a given user on facebook. I have requested the user_location permission and my graph API call also requests location however I am only ever returned the city and an ID for the location - never an actual country.
My requests etc are below. I am using the standard PHP SDK docs
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me?fields=birthday,name,statuses,photos,location' );
$response = $request->execute();
// get response
$response = $response->getGraphObject();
$data_we_need = array();
$data_we_need['name'] = $response->getProperty('name');
$data_we_need['birthday'] = $response->getProperty('birthday');
$data_we_need['location'] = $response->getProperty('location');
$statuses = $response->getProperty('statuses');
$data_we_need['statuses'] = $statuses->asArray();
$photos = $response->getProperty('photos');
$data_we_need['photos'] = $photos->asArray();
I am returned an results like:
[name] => xxxxxx
[birthday] => 05/14/1990
[location] => __PHP_Incomplete_Class Object
(
[__PHP_Incomplete_Class_Name] => Facebook\GraphObject
[backingData:protected] => Array
(
[id] => 112087812151796
[name] => Gloucester, Gloucestershire
)
)
I need to be able to get country from the location data provided.
Any help would be massively appreciated.
As far as I know the location & hometown fields are user inputs (community pages), hence you won't get stable results using the facebook API. You might rather want to try detecting the country yourself with the IP.

How can I show a list of *all* available calendars using Google Calendar API v3 / Google API Client Library?

I have been trying to access the Google Calendar API v3 using PHP. Initially, I want to simply list the user calendars that are accessible to my call to the API.
To do so, I have downloaded the Google API PHP Client Library and have attempted to use the following code (which is sourced, with my adaptations, from https://mytechscraps.wordpress.com/2014/05/15/accessing-google-calendar-using-the-php-api/ ):
<?php
//error_reporting(0);
//#ini_set('display_errors', 0);
// If you've used composer to include the library, remove the following line
// and make sure to follow the standard composer autoloading.
// https://getcomposer.org/doc/01-basic-usage.md#autoloading
require_once './google-api-php-client-master/autoload.php';
// Service Account info
$client_id = '754612121864-pmdfssdfakqiqblg6lt9a.apps.googleusercontent.com';
$service_account_name = '754674507864-pm1dsgdsgsdfsdfsdfdflg6lt9a#developer.gserviceaccount.com';
$key_file_location = 'Calendar-7dsfsdgsdfsda953d68dgdsgdsff88a.p12';
$client = new Google_Client();
$client->setApplicationName("Calendar");
$service = new Google_Service_Calendar($client);
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/calendar.readonly'),
$key
);
$client->setAssertionCredentials($cred);
$cals = $service->calendarList->listCalendarList();
print_r($cals);
?>
I have created a service account in the Google Developers console, and generated OAuth details, which I have used to set the appropriate variables as can be seen from the code.
This code returns the following:
Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => "14171313334000" [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => calendar#calendarList [nextPageToken] => [nextSyncToken] => 000014121268327000 [modelData:protected] => Array ( [items] => Array ( [0] => Array ( [kind] => calendar#calendarListEntry [etag] => "1417721316542000" [id] => bots#madeupcompany.co.uk [summary] => bots#madeupcompany.co.uk [timeZone] => Europe/London [colorId] => 23 [backgroundColor] => #cd74e6 [foregroundColor] => #000000 [selected] => 1 [accessRole] => reader [defaultReminders] => Array ( ) ) ) ) [processed:protected] => Array ( ) )
The problem is that this seems to be returning details of only one calendar. That is, the calendar for bots#madeupcompany.co.uk (the only one I explicitly shared with my service).
However, I know this google account has read-only access to a number of other user's calendars (I can see them when logged into Google Calendar as this user).
Furthermore, if I use the Google Apps Explorer on this page: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list#auth , while logged into my google account as bots#madeupcompany.co.uk, I get details of all of these other calendars.
So I'm trying to work out, why is the Apps Explorer showing me all the other calendars, and yet the PHP code is not?
A service account doesn’t need to prompt a user for access because you have to set it up. Go to the Google Calendar website. Find the Calendar Settings , then go to the Calendars tab, find the calendar you want to access and click on “Shared: Edit settings” add the service account email address like you would a persons email address. This will give the service account the same access as if you where sharing it with any other user.
<?php
session_start();
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';
/************************************************
The following 3 values an befound in the setting
for the application you created on Google
Developers console. Developers console.
The Key file should be placed in a location
that is not accessable from the web. outside of
web root.
In order to access your GA account you must
Add the Email address as a user at the
ACCOUNT Level in the GA admin.
************************************************/
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
$Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp#developer.gserviceaccount.com';
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes ="https://www.googleapis.com/auth/calendar.readonly";
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Calendar($client);
?>
<html><body>
<?php
$calendarList = $service->calendarList->listCalendarList();
print_r($calendarList);
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo "<a href='Oauth2.php?type=event&id=".$calendarListEntry->id." '>".$calendarListEntry->getSummary()."</a><br>\n";
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
?>
</html>
code ripped from tutorial Google Calendar API with PHP – Service Account

Google Api get users Email Address

I am using the Google Api PHP client to Log the user in. I want to get the Users Email address. I have the following code.
Scope:
$client->setScopes('https://www.googleapis.com/auth/userinfo.profile');
Code:
$request = new apiHttpRequest("https://www.googleapis.com/oauth2/v2/userinfo?alt=json");
$userinfo = $client->getIo()->authenticatedRequest($request);
$response = $userinfo->getResponseBody();
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
This give me only the following details
Array
(
[id] => 110084312800396764
[name] => Harsha M V
[given_name] => Harsha M
[family_name] => V
[picture] => https://lh6.googleusercontent.com/-Xusc8lwgLIQ/AAAAAAAAAAI/AAAAAAAAAAA/sOthy23uJGk/photo.jpg
[locale] => en
)
Setting the scope in an array does the job.
$this->client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
https://www.google.com/accounts/AuthSubRequest?next=http://www.example.com/index.php&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/analytics.readonly&secure=0&session=1
or
https://www.google.com/accounts/AuthSubRequest?next=http://www.example.com/index.php&scope=email+https://www.googleapis.com/auth/analytics.readonly&secure=0&session=1
both of above links works for me in php

Categories