Authorizing WordPress Plugin to use Gmail API on Google Cloud Platform

software: WordPress
cloud: Google Console
plugin: WP Mail SMTP

The Problem

Google is constantly changing the layout of their backend and also modifying how users authorize apps to use their APIs. It seems like it’s ever increasingly complicated, but I digress.

With any website, it is nice to have the capacity to send mail. This is useful for contact forms, but also for error messages and automated scripts. One can use the built in PHPmail() function, but connecting with proper SMTP mailer is ideal, in order to have proper security and avoid getting emails bounced.

WordPress now has many plugins that allow you to use the Gmail API to send mail from your website. Problem is that Google, at the time of writing this, has pretty strict authentication standards: OAuth 2.0 protocol.

For this solution, we’ll be using the WP Mail SMTP plugin, but there are others that authenticate in the same way.

The Solution

First, install WP Mail SMTP from your ‘Add New Plugin’ section of the dashboard.

Add a new plugin in WordPress

Quick search for ‘gmail’ will give a list of all the plugins to install, I like to use WP Mail SMTP.

Once activated, you can browse to the Settings page of the plugin and start entering in the details.

Select Gmail Mailer to begin setting up Google API

Fairly simple. Much like any other plugin install. Except now, you’ve got to enter the sphere of Google Cloud Platform, a confusing, convoluted world of programmers.

WP Mail SMTP has a configuration step by step guide that used to work, except now it’s not up to date with how to add credentials.

Google Cloud Platform is where you want to go now. Here you’ll give WP Mail SMTP access to use your Gmail account.

First, you’ll need to create a project. Name this project something meaningful, you might have to reference it again.

Once it’s created, make sure you’ve got it selected from the drop down menu at the top of the screen, and navigate to the APIs and Services tab and select Credentials.

From here, select Create Credentials from the top and select OAuth.

Create API Authentication Credentials to use in WordPress Mailer

You’ll be directed to the consent screen for OAuth, this will be a web application.

Create OAuth Client ID in Google Cloud

Here you only need to add the redirect URI given to you by your plugin, in this case it will be

https://connect.wpmailsmtp.com/google/

from WP Mail SMTP settings page on your WordPress site. Click create and you’re almost there.

Head into the OAuth Consent Screen tab, where you want to move this now into published mode.

Publish the API that now has OAuth authentication

After this, you need to now give the Gmail API authority to make emails on your behalf. To do this, you have to navigate to the Library within your APIs & Services screen and search for Gmail API.

Select the Gmail API and enable the service.

To finally get the Client ID and Client Secret, click on the Credentials tab and you will see under the OAuth 2.0 Client IDs will be the ID you just created. On the right hand side will be a little button to edit the key, if you click that, you will be able to find your Client ID and Client Secret that you will have to copy/paste into the WordPress Plugin WP Mail SMTP settings.

Copy and Paste Client ID and Client Secret into WP Mail SMTP settings within WordPress

Once you’ve copy/pasted the settings into the WordPress WP Mail SMTP settings page, click save settings and your page will refresh and this section will now have a big orange button asking you to allow the plugin to send emails using your Google account.

After clicking that big orange button, you will be asked to authenticate yourself using your Google account, make sure to select the right Google account to use for this, which is the account you used to setup the whole API Key above from the start.

Once you have successfully linked WP Mail SMTP with Google API, you are now ready to send a test email to see if it’s actually working.

Conclusions

This is becoming quite tedious to setup Gmail to use as your website emailer. It would seem more logical to take a relaxing walk around the block than to go through with this again!

However, remember that Google does this all to make sure that the emails you’re sending are actually from you and not hijacked to create spam. And also remember that you can only send so many emails using the Gmail API in a day, so be wary about email lists that a gluttonous.

It is also important to have your SPF, DKIM, and DMARC all configured if possible. This is done by setting the adding DNS records that you get from your Gmail admin console. The helpful links are beneficial to follow.

Troubleshooting

You may get an error message when you test with an email:

"error": {
    "code": 403,
    "message": "Gmail API has not been used in project XXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/gmail.googleapis.com/overview?project=XXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",

If you do get this, follow the link it provides you to solve all your problems.




RAW Images in Lightroom Show Blank Screen

plugin: Adobe Camera Raw
program: Adobe Lightroom

The Problem

So, I got a new computer and installed Lightroom and imported all my photographs to get my new computer up to speed. I was able to view the images in the Library, but when I went into Develop, the viewing screen went gray/blank. This only happened with my original *.RAW/*.AWR photographs. With the JPG exports I had already created when editing the images and imported just now in a subfolder of the original photographs, they would all show up nicely without an issue.

It should be obvious that the *.RAW / *.AWR or whatever proprietary RAW file format your camera makes wasn’t recognized in Lightroom. Obviously this is an issue for most photographers since photographing in RAW is essential for maximum colorspace to be captured.

The Solution

Adobe has a program called Camera Raw. Download can be gathered here. It is a plugin for most of the Adobe suite that provides support for RAW files from your camera.

Raw files contain the most complete, uncompressed image data but in a form that is not directly usable and must be processed.” ~ Borrow Lenses

Take special note of the note at the top:

For older Adobe applications, substitute with the Camera Raw 10.5 installer below.

There are pretty clear steps on the Adobe site and which installer you should choose there.

Hope this helps!




Python Error: Unsupported Pickle Protocol 5

python ide: Spyder
os: Windows
package: Pickle5
package: Pickle

Introduction

In a previous article on trying to discover why a program would run in Spyder, and then when I ran it in IDLE or from command prompt it would fail saying that the packages weren’t installed. The skinny of how to resolve this issue was from command prompt type:

pip install <package name>

The explanation of this is that when one installs Spyder, it has built in Python Interpreter, which can be discovered within Sypder -> Tools -> Preferences.

Within the console of Spyder, one can also run the command:

import sys
sys.executable

and then from command prompt (or IDLE) you can run the same command (if you’re using command prompt, you’ll have to first enter into Python using the python command, which should be recognized because you have linked this command to a PATH or PYTHONPATH in your environment variables). If the two system paths match up (the location of python.exe), the exact same python packages will be used by both IDLE and Spyder.

However, if the system paths don’t match up it is likely that the two IDEs are running different Python environments.

The Problem

After I installed the necessary packages that Spyder had come installed with that I didn’t have installed with Python I had separately installed, my program was able to run.

However, my program modified files, and because the two versions of Numpy that were being used were different, the following error showed up:

Python Error: Unsupported Pickle Protocol 5

The Solution

The simple solution may have been to install the package pickle5:

pip install pickle5

which:

This package backports all features and APIs added in the pickle module in Python 3.8.3, including the PEP 574 additions. It should work with Python 3.5, 3.6 and 3.7.

and then used that in my program running in Spyder where the Pickle error was being experienced:

import pickle5 as pickle

However, when trying to install pickle5 I experienced the issue:

error: Microsoft Visual C++ 14.0 is required. Get it with “Build Tools for Visual Studios”: https://visualstudio.microsoft.com/downloads/

I went about downloading this package, but it appeared it was going to download the whole Visual Studio which was about 1.5Gb and as a general rule I try not to clutter up my computer with this kind of massive downloads (like Visual Studio is). Although, likely as a programmer you’ll have to have this in some shape installed on your computer.

So, why not just change the Python Interpreter to the one I first installed on my computer, outside of Spyder, the same one IDLE runs. For me, this was the location: C:\python\python39\python.exe (as shown in the image above, and discovered with the command prompt sys.executable report we did above).

If you do this, and restart Spyder, you’ll likely get an error saying that the console was unable to load, and to install the Spyder kernels. This command solves that:

pip install spyder-kernels

One thing I learned from all of this was that Spyder recommends the programmer to install and setup Spyder using Anaconda (though they do have a curious Important note at the top of that page explaining installing Spyder without Anaconda might be beneficial to avoid package dependency conflicts, as we have just experienced issues with being independent ironically!). I’m not sure if all of this would have been eliminated as a result of installing Spyder using Anaconda, because when I created a scheduled task I was then running the program outside of Spyder. However, I’m sure that there is a way to install Spyder such that it uses the existing Python interpreter installed on the system, which would be using the same Python interpreter from the beginning, and it hopefully would have installed the required site-packages in the same folder.

Hope this helps!




Troubleshooting Task Scheduler Running a Python Script

program: Python
package manager: PIP
ide: Spyder

The Background

Creating an automated or scheduled task on your computer in Windows or Mac is pretty awesome for saving time doing routine tasks, particularly routine scripts or program runs. However, when setting up the task in the Task Scheduler (Windows), there isn’t much of a GUI that shows what is happening with the program like there would be in an IDE.

I was setting up a Python script to automate. To complicate things, I use Spyder IDE to code in.

The Problem

I created a *.bat file. Double clicked on the file to run the script, and a window popped up and quickly closed.

I doubled checked in Spyder to see if the program ran, sure enough, ran like a charm.

The Solutions

Yes, plural solutions. There were lots of things wrong.

First

The biggest problem was that Spyder seems to have it’s own package folder that has some built in packages like Numpy, Matplotlib, and Pandas. I’m sure there are others. So, when I run my program in Spyder, everything ran great. But when I ran it using my *.bat file, it was running without the IDE and therefore didn’t have the built in scripts.

To figure this out, I opened the *.py file (my program) using IDLE, then ran (F5). The shell told me:

import numpy
ModuleNotFoundError: No module named ‘numpy’

Second

pip install numpy

That’s the short answer.

The long answer is to systematically install each package that had been assumed to be installed in Python.

This is done by continuing to run the Python script to see why it crashes. This will happen with similar ‘ModuleNotFoundError’s as seen above. Each module not found, pip install that package.

pip install <package name>

The above command is typed directly into Command Prompt, if pip has been installed correctly.

One the program runs completely within IDLE, it should now run smoothly in the Task Scheduler.

Third

Setting up a Task Scheduler might be a bit confusing. It’s an app or program on Windows now of the same name. It’s also found in the directory:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

When you click create task you will be stepped through a few tabs to create the new task.

Name the task, select whether you need to be logged in for it to run or not, set your triggers which can be time of day or specific days, and action is the tab that holds the information for what program to run.

Create a *.bat File

Head to this other post I’ve made on How To Create a *.bat File in Windows For Task Scheduler To Run Python Script. It should give you all the details.

Creating a *.bat file to run your script is probably the easiest way to create your action in the Task Scheduler. Once your *.bat file is created, I would suggest going to the actual *.bat file and double clicking on it to confirm that it does work.

tip: insert some print code or a sleep() statement at the end of your code so that you can get a confirmation that it has actually run. Sometimes the programs run pretty quick and you don’t get any feedback that it’s run.

If your *.bat file runs properly, in the Action tab of the Task, add a new action, and browse to the *.bat file you created above and select ‘start a program’ as the action you’d like to accomplish.

This should now show up as a task in your task scheduler. To see if it’s going to work before you wait for your triggers to be hit, right click on the task and select run.

Hope this helps you troubleshoot your scheduled task in Windows.




How To Create a *.bat File in Windows For Task Scheduler To Run Python Script

If you would like to run your Python script without having to open your IDE to complete a task, you can do so by creating a *.bat file. I’d be interested to hear what other reasons why you found this page to create your *.bat file in the comments below, because I’m sure there are some pretty cool tasks out there you are doing.

The format of the *.bat file is as:

“<location to python.exe file>” “<location of your python.py script you want to run>”

For me, this looked like:

“C:\Python\Python39\python.exe” “C:\Python\Python39\programs\coolProgram.py”

Saving the File

Saving the file is the tricky part.

Make sure you have the drop down save as type selected to *.*. If you don’t your file will save as filename.bat.txt.

To avoid any file location issues, I’d suggest saving this *.bat file in the same location you have your python script in.