Site icon Learn Web Development & Programming, Artificial Intelligence, WordPress, Shopify Articles

Export Gmail Contacts in PHP

export gmail contacts in php

Hello! In this post i will show you how to export gmail contacts in PHP after entering the gmail credentials using google oauth2. It will display all google contacts of that particular Email account and also i will give you option to from where you can download those contact in excel sheet. This whole google OAuth cycle is basically based on how to access the third party data. After passing through a strong authorization, we get some access permission with the help of client id and password. And that data is used for getting access token and code and that key is used for further accessing the complete details by accessing the google API.

Before going to complex coding part  just have  a look over these basic terminology listed below.

 Google OAuth:

OAuth is known as  “open standard for authorization” and provide a strong authorization scheme on behalf of owner. OAuth was started with oauth1.0, but it has been deprecated  and replaced with oauth2.0. You have to make sure that you are using oauth2.0

OAuth Code: We only get this code while we are successfully authorized. After getting OAuth Code code we can request for the access token.

Access Token: This token is key to get the content from the website server. We send request for the access token with authorized code after this we get the access token. With this token, we can access most of the API and also  we can collect the  data according to our requirements.

Refresh Token: Refresh token is not a separate token ( Often! we are confused with refresh the token ) even though it is same access token. But it’s value got refreshed after leaving  one complete flow or logout from your session.

So Simply get the access token and maintain its session  until you get logout and  the next time when you try to re-login, you will get a new access token which is known as refresh token .

Export gmail contacts in php

Steps for getting the google contacts :  here we are explaining step-by-step process for accessing the google contacts.

Step :1

In this step you have to make one google  oauth registration  in google developer console for accessing the google api, please visit ‘https://console.developers.google.com/project  to create google api and follow these steps.

  1. Go to the Google Developers Console.
  2. Select a project, or Create New Project. 
  3. Type your project name in the Project name field.
  4. In the Project ID field, optionally type in a project ID for your project. This ID must be unique world-wide.
  5. Click on Create button and wait for the project to be created.
  6. Click on the new project name to start editing the project.
  7. Under “APIs & auth” In the sidebar , select Consent screen.
  8. Choose an Email and specify a Product Name.
  9. Under “APIs & auth” In the sidebar, select Credentials.
  10. Click on Create a new Client ID .
  11. Register the origins from which your app is allowed to access the Google APIs. An origin is a unique combination of protocol, hostname, and port.
  12. select Web application,  In the Application type section of the dialog .
  13. Enter the origin for your app In the Authorized JavaScript origins field. You can enter multiple origins to allow for your app to run on different protocols, domains, or subdomains.
  14. In the example below, Wildcards are not allowed.  the second URL could be a production URL.
    http://localhost:8080
    https://myproductionurl.example.com
  15. Delete the default value from the Authorized redirect URI field. Redirect URIs are not used with JavaScript APIs.
  16. Click on the Create Client ID button.
  17. Copy the Client ID ( In the resulting Client ID for web application section ) that your app will need to use to access the APIs.

just after completion you will get a client id and secret key.

Step :2

we have already gotten the client id and secret key. Now, we will use these credential for login window or login page ‘index.php’  while we execute this page. We will get window asking for ‘sign in with google ‘ when  we click on this button. We will sent to gmail login window and after entering the gmail credential it directly redirected to our google API. Here user is asked for giving Allow and deny the access. If he click on Allow button, he is redirected to the callback page here we can see our google contacts and can import them in excel sheet.

index.php

Step :3

Here you will display all the gmail contacts , we are using access token for accessing the google contacts.

callback.php

csvdownload.php

This script will show you how to download the export google contacts in excel sheet , we are getting the data as string and here we are using scripts for printing them for spread sheet format.

 

Conclusion :

After reading the above post, I am sure you will give a try to the script provided and implement it in your own projects as well. Feel free to visit our website again in the future to get in touch with new coding tricks. You can let us know about your feedback in the space provided below

Exit mobile version