DevCEDTeam / CED

0 stars 0 forks source link

Description #35

Open DevCEDTeam opened 1 year ago

DevCEDTeam commented 1 year ago

To integrate the gCloud Gmail API with the Mautic #FOSOAuthServerBundle and send outbound email messages from Mautic through the Gmail API, follow these step-by-step instructions:

DevCEDTeam commented 1 year ago
  1. Install FOSOAuthServerBundle:

    • Open your composer.json file and add the following requirement:
      {
       "require": {
           // ...
           "friendsofsymfony/oauth-server-bundle": "^2.4"
       }
      }
    • Run the composer update command to install the bundle:
      composer update friendsofsymfony/oauth-server-bundle
  2. Enable the bundle:

    • Open the app/AppKernel.php file.
    • Add the following line to the registerBundles method:
      new FOS\OAuthServerBundle\FOSOAuthServerBundle(),
DevCEDTeam commented 1 year ago
  1. Create model classes:
    • In your Symfony project, create the following model classes: Client.php, AccessToken.php, RefreshToken.php, AuthCode.php.

To create the model classes in your Symfony project, you can follow the following step-by-step instructions:

(1). First, make sure that you have installed the FOSOAuthServerBundle in your Symfony project. You can install it via Composer by running the following command in your terminal:

   composer require friendsofsymfony/oauth-server-bundle

(2). Next, create the AppBundle/Entity directory in your Symfony project. This is where we'll store our model classes.

(3). Inside the AppBundle/Entity directory, create the following PHP files:

(4). Add the following code to each of the PHP files you just created. You can customize the properties and methods to fit your specific needs:

   // Client.php
   namespace AppBundle\Entity;

   use FOS\OAuthServerBundle\Entity\Client as BaseClient;
   use Doctrine\ORM\Mapping as ORM;

   /**
    * @ORM\Entity
    */
   class Client extends BaseClient
   {
       /**
        * @ORM\Id
        * @ORM\Column(type="integer")
        * @ORM\GeneratedValue(strategy="AUTO")
        */
       protected $id;
   }
   // AccessToken.php
   namespace AppBundle\Entity;

   use FOS\OAuthServerBundle\Entity\AccessToken as BaseAccessToken;
   use Doctrine\ORM\Mapping as ORM;

   /**
    * @ORM\Entity
    */
   class AccessToken extends BaseAccessToken
   {
       /**
        * @ORM\Id
        * @ORM\Column(type="integer")
        * @ORM\GeneratedValue(strategy="AUTO")
        */
       protected $id;
   }
// RefreshToken.php
namespace AppBundle\Entity;

use FOS\OAuthServerBundle\Entity\RefreshToken as BaseRefreshToken;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class RefreshToken extends BaseRefreshToken
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO
DevCEDTeam commented 1 year ago
  1. Create model classes:

    • Adjust the namespaces and file locations as per your project's structure.

(1) . Determine the root namespace of your project. This will be the top-level namespace that all other namespaces in your project will be based on. For example, if your project is called "MyProject", your root namespace might be "MyProject".

(2) . Decide on a directory structure for your project. This will involve creating directories and sub-directories to organize your code files. For example, you might create a directory called "src" to hold your source code files, and sub-directories inside "src" to organize related classes.

(3). Adjust the namespaces in your code files to match your directory structure. The namespace of each file should reflect its location in the directory structure, with the root namespace followed by sub-namespaces for each directory level. For example, if you have a file called "MyClass.php" in a sub-directory called "models", and your root namespace is "MyProject", the namespace for that file might be "MyProject\Models\MyClass".

(4). Update any references to the old namespaces in your code. This may involve updating "use" statements, class names, and other references to the old namespaces to match the new namespace structure.

(5). Organize any autoloading or namespace mapping configurations to match your new structure. This typically involves modifying your project's autoloading configuration to register any new namespaces that you've added, and to adjust any mappings between namespaces and directory paths that may be in place.

Overall, the goal is to create a clear and logical structure for your code files that makes them easy to find and understand, while also ensuring that namespaces accurately reflect that organization.

DevCEDTeam commented 1 year ago
  1. Configure your application's security.yml:

    • Open the app/config/security.yml file.
    • Add the following configuration for the FOSOAuthServerBundle:

      security:
       firewalls:
           oauth_token:
               pattern:    ^/oauth/v2/token
               security:   false
      
           oauth_authorize:
               pattern:    ^/oauth/v2/auth
               # Add your favorite authentication process here
      
           api:
               pattern:    ^/api
               fos_oauth:  true
               stateless:  true
               anonymous:  false # can be omitted as its default value
      
       access_control:
           - { path: ^/api, roles: [IS_AUTHENTICATED_FULLY] }

      Note: Adjust the firewall patterns (^/oauth/v2/token, ^/oauth/v2/auth, ^/api) and access control as per your project's requirements.

  2. Configure FOSOAuthServerBundle:

    • Open the app/config/routing.yml file.

    • Import the routing configuration for the FOSOAuthServerBundle:

      # app/config/routing.yml
      fos_oauth_server_token:
       resource: "@FOSOAuthServerBundle/Resources/config/routing/token.xml"
      
      fos_oauth_server_authorize:
       resource: "@FOSOAuthServerBundle/Resources/config/routing/authorize.xml"
    • Open the app/config/config.yml file.

    • Add the FOSOAuthServerBundle configuration: Certainly! Here's the continuation of the configuration steps:

      app/config/config.yml

      fos_oauth_server: db_driver: orm # or pdo_mysql if you use MySQL client_class: YourBundle\Entity\Client access_token_class: YourBundle\Entity\AccessToken refresh_token_class: YourBundle\Entity\RefreshToken auth_code_class: YourBundle\Entity\AuthCode service: user_provider: fos_user.user_provider.username_email

  3. Generate the database tables:

    • Run the following command to generate the necessary database tables for the OAuth server:
      php bin/console doctrine:schema:update --force
  4. Create the Gmail API client:

    • Go to the Google Cloud Console (https://console.cloud.google.com/).
    • Create a new project or select an existing one.
    • Enable the Gmail API for your project.
    • Create API credentials (OAuth 2.0 client ID) for your project.
    • Download the JSON file containing the client credentials.
  5. Configure the Gmail API client credentials:

    • Rename the downloaded JSON file to client_secret.json.
    • Move the client_secret.json file to the app/ directory of your Symfony project.
  6. Configure the Gmail API integration:

    • Open the app/config/config.yml file.
    • Add the following configuration for the Gmail API:
      # app/config/config.yml
      google_api:
       client_id: Your_Client_ID
       client_secret: Your_Client_Secret
       redirect_uri: Your_Redirect_URI
       scopes:
           - https://www.googleapis.com/auth/gmail.send

      Replace Your_Client_ID, Your_Client_Secret, and Your_Redirect_URI with the corresponding values from the client credentials you obtained in step 7.

Your_Redirect_URI : https://mautic.cfored.com/s/plugins/oauth2callback

  1. Implement the Gmail API integration:

    • In your Symfony project, create a service or class responsible for handling the Gmail API integration.
    • Use the Gmail API client library (such as google/apiclient) to authenticate and send emails via the Gmail API.
    • Follow the Gmail API documentation and library documentation to implement the necessary logic for sending outbound emails.
  2. Test the integration:

    • Create a test route or controller action to trigger the email sending functionality.
    • Make a request to the test route and verify that the emails are being sent successfully using the Gmail API.

Remember to adjust the configurations and code examples according to your specific Symfony project structure and requirements.

.