## Python Example
```python
import os
import subprocess
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
def get_api_key():
try:
# Retrieve the API key from 1Password using the CLI
result = subprocess.run(
['op', 'item', 'get', 'Google Tasks API Key', '--fields', 'api_key'],
capture_output=True, text=True, check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error retrieving API key from 1Password: {e}")
return None
def add_task(task_title):
api_key = get_api_key()
if not api_key:
print("API key is missing. Please check your 1Password setup.")
return
# Create a Google Tasks service object
service = build('tasks', 'v1', developerKey=api_key)
# The task to be added
task = {
'title': task_title,
}
try:
# Insert the task into the default task list
result = service.tasks().insert(tasklist='@default', body=task).execute()
print(f"Task created: {result.get('title')}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
task_title = input("Enter the task title: ")
add_task(task_title)
```
### Explanation
1. **Get API Key from 1Password**: The `get_api_key` function uses the 1Password CLI (`op`) to retrieve the API key stored in a 1Password item named "Google Tasks API Key". The `--fields api_key` option specifies that we're interested in the `api_key` field of that item.
2. **Create Google Tasks Service**: The `build` function from the `googleapiclient.discovery` module creates a service object for the Google Tasks API, using the retrieved API key.
3. **Add Task to Google Tasks**: The `add_task` function creates a new task with the given title and inserts it into the default task list.
4. **Handle Errors**: The script includes basic error handling to manage issues with retrieving the API key or interacting with the Google Tasks API.
## Ruby Example
```ruby
#!/usr/bin/env ruby
require 'google/apis/tasks_v1'
def get_api_key
# In Ruby, the backticks (` `) are used to execute a command in the shell and return the standard output of that command as a string. This is also known as command substitution. The result of the command executed within the backticks is captured as a string, which can then be used
`op item get "Google Tasks API Key" --fields api_key`.strip
end
def add_task(task_title)
api_key = get_api_key
return puts "API key is missing. Please check your 1Password setup." if api_key.empty?
service = Google::Apis::TasksV1::TasksService.new
service.key = api_key
task = Google::Apis::TasksV1::Task.new(title: task_title)
result = service.insert_task('primary', task)
puts "Task created: #{result.title}"
rescue => e
puts "An error occurred: #{e.message}"
end
print "Enter the task title: "
task_title = gets.chomp
add_task(task_title)
```