Skip to content
Advertisement

Send email by Google API

I trying to send an email using Google API

Send email controller look as

JavaScript

getClient class:

JavaScript

When I’m trying to run this I receiving error:

JavaScript

credentials.json

JavaScript

What can be wrong with my code? When I’m trying to print out $client it displays required data. Or how I shall to login to use it properly without interruptions? I’ve logged in before(several hours ago).

Advertisement

Answer

It looks like you’re missing the step where you obtain and/or set the access token or refresh token. In the PHP Quickstart for Gmail, it’s this chunk of code:

JavaScript

To break it down a bit:

  1. You need to request authorization from the user by sending them to the authorization URL ($client->createAuthUrl())
  2. then exchange the code for an access token ($client->fetchAccessTokenWithAuthCode($authCode))
  3. and either set it on your client ($client->setAccessToken($accessToken)) or save it for future use.

In addition to the access token, you’ll also want to save the refresh token that gets returned so you don’t have to re-login every time ($client->fetchAccessTokenWithRefreshToken($refreshToken)). If you’ve gone through this process already, it’s possible that your access token expired and you need to either use the refresh token or re-authenticate to get access again.

If you’re developing this application for more users than just yourself, you may want to look into managed OAuth platforms like Xkit, where I work. They handle the process of getting authorization, refreshing tokens, etc, along with encrypting the tokens and storing them for each individual user.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement