q parameter error for listUsersDrafts in Google API PHP client
Today morning I was working on an interesting task. It was to login to one's Gmail account and get their details like Inbox, Labels, Drafts and save it in another place. That was interesting. But when I was trying to get some particular drafts by searching (using the API), I got an error in my script.
The Gmail API provides the facility to query the drafts list using a keyword and get the filtered result. This is done by passing an optional parameter "q" with the search query in the API request. This is documented in the Gmail API reference page.
So after seeing this, I tried to pass the "q" parameter in my API request using the PHP client library, but my script failed giving an error. The error was "(list): unknown parameter 'q'". I understood that the library is probably missing the support for this parameter. I dug a little bit into the client library and found my assumption was correct. Luckily this was easy to fix. All I had to do was edit one library file and add a few lines to it.
Although this fix worked and solved my problem, this is not something I would normally do. Editing core files of a library is not recommended mainly because when you update the library, your changes might disappear and the problems will arise again. But in this case, I proceeded with it because this task was not a part of a serious project and I did it for myself because I was bored ;) If you would prefer to go with this method, here is how I fixed it.
First let's see how we call the Gmail API using the client library to get the user's drafts. I hope you know how to authenticate and get the draft's list already. Here I'm focusing only on the q parameter and filtering the drafts list. So only the necessary lines of codes are shown below.
CASE 01: This is how to list all drafts.
$draftsResponse = $service->users_drafts->listUsersDrafts($userId);
This should work really fine.
CASE 02: Searching and filtering drafts using q parameter.
$params = array(
'q' => "search keyword"
);
$draftsResponse = $service->users_drafts->listUsersDrafts($userId, $params);
Here we pass an optional parameter $params, which is an array having the key "q" with our query to the listUsersDrafts() method. If you use the client library downloaded as of today, it will give you an error because listUsersDrafts() method does not know any option as q. So in order to fix this, all you have to do is;
Open file google-api-php-client/src/Google/Service/Gmail.php and look for this block.
$this->users_drafts = new Google_Service_Gmail_UsersDrafts_Resource(
.
.
.
.
,'list' => array(
'path' => '{userId}/drafts',
'httpMethod' => 'GET',
'parameters' => array(
'userId' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'maxResults' => array(
'location' => 'query',
'type' => 'integer',
),
'pageToken' => array(
'location' => 'query',
'type' => 'string',
)
),
)
.
.
.
It should look like this. Locate the above code block inside. Now we need only need to add additional q parameter here. Then it will look like this one.
.
.
.
.
,'list' => array(
'path' => '{userId}/drafts',
'httpMethod' => 'GET',
'parameters' => array(
'userId' => array(
'location' => 'path',
'type' => 'string',
'required' => true,
),
'maxResults' => array(
'location' => 'query',
'type' => 'integer',
),
'pageToken' => array(
'location' => 'query',
'type' => 'string',
),
'q' => array(
'location' => 'query',
'type' => 'string',
)
),
)
.
.
.
That's it!! now check your script again and it will work without giving you any errors and it will filter the drafts using the given keyword :) I've already created an issue in Google Client Library GitHub project and created a pull request with this fix. Hopefully they will add this in a future release. If they do not accept it, you can still use this method.
I hope this will help someone. Please do not forget to share this with your friends in Google+, Facebook and Twitter etc if you think this will be helpful to someone you know.!! Feel to leave a comment and let me know what you think about my article.