[Python] Request to create a note in Google Keep returns invalid argument

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create notes in Google Keep using API, here is the python script:

    import sys
    from google.oauth2.service_account import Credentials
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError

    # Define the required scopes for Google Sheets and Keep
    SCOPES = [
    'https://www.googleapis.com/auth/keep',
    ]

    # Path to your service account JSON key file
    SERVICE_ACCOUNT_FILE = 'service_file.json' # Replace with your file path

    def authenticate():
    """Authenticates with Google Sheets and Keep using a service account."""
    try:
    creds = Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES
    )
    return creds
    except Exception as e:
    print(f"Error during authentication: {e}")
    return None

    def create_keep_note(creds):
    """Creates a note in Google Keep.

    Args:
    creds: The authenticated credentials.
    """
    try:
    service = build('keep', 'v1', credentials=creds)
    note = {
    "title": "title",
    "body": {
    "text": {
    "text": "body"
    }
    }
    }
    service.notes().create(body=note).execute()
    print(f"Note '{title}' created in Google Keep.")
    except HttpError as e:
    print(f"Error creating Keep note: {e}")
    except Exception as e:
    print(f"An unexpected error occurred: {e}")

    if __name__ == '__main__':
    creds = authenticate()
    if not creds:
    sys.exit("Authentication failed. Exiting.")

    create_keep_note(creds)
    print("Completed processing.") #Added to confirm completion


    but I'm getting strange error:


    Error creating Keep note: <HttpError 400 when requesting https://keep.googleapis.com/v1/notes?alt=json returned "Request contains an invalid argument.". Details: "Request contains an invalid argument.">
    ​

    I'm following Google Keep API so not sure what's wrong with my request.

    Continue reading...

Compartilhe esta Página