mardi 3 octobre 2023

How to authenticate and access the Gmail API in Ruby without using OOB

How do I connect to the api without using oob since it is obsolete, I have been searching and I can't find any example. Alguna ayuda

These are the methods for OOB but I don't need to use them as they are deprecated.

type here
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'google/apis/gmail_v1'

# Constants for authentication
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Your Gmail Application'
CLIENT_SECRETS_PATH = 'path/to/client_secrets.json'.freeze
CREDENTIALS_PATH = 'path/to/credentials.yaml'.freeze
SCOPE = Google::Apis::GmailV1::AUTH_SCOPE

# Configure authentication
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE)
credentials = authorizer.get_credentials('user_id', token_store)

Step 2: Credential Verification
In this step, we'll check if we already have stored credentials or if we need user authentication.

# Check if credentials exist or if user authentication is needed
if credentials.nil?
  url = authorizer.get_authorization_url(base_url: OOB_URI)
  puts 'Open the following URL in your browser and enter the authorization code:'
  puts url
  code = gets
  credentials = authorizer.get_and_store_credentials_from_code(
    user_id: 'user_id', code: code, base_url: OOB_URI, token_store: token_store
  )
end

Aucun commentaire:

Enregistrer un commentaire