Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

Authorize a desktop application to use Gmail

See more GMail REST API Examples

Enable the Gmail API, configure the OAuth consent screen, and create a Desktop app OAuth client. Launch the system browser and receive the authorization code on a loopback listener. The sample requests read-only access with PKCE and offline access. For another operation, select the scope in that example before authorizing. Gmail scopes.

Run in a private, user-only directory. The file contains bearer credentials. In a deployed application use protected credential storage. To reuse an existing grant, copy the older examples’ qa_data/tokens/gmail.json to gmail-token.json in this private directory. The token retains its original scopes. Gmail clients using IMAP, POP, or SMTP with OAuth need the https://mail.google.com/ scope. Use a separate cache for each user and client.

Google documentation

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoOauth
    String sAuthUrl
    Integer iWaited
    Handle hoToken
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    Get Create (RefClass(cComChilkatOAuth2)) To hoOauth
    If (Not(IsComObjectCreated(hoOauth))) Begin
        Send CreateComObject of hoOauth
    End
    Set ComTokenEndpoint Of hoOauth To "https://oauth2.googleapis.com/token"
    Set ComClientId Of hoOauth To "YOUR_DESKTOP_CLIENT_ID"
    Set ComClientSecret Of hoOauth To "YOUR_DESKTOP_CLIENT_SECRET"

    Set ComAuthorizationEndpoint Of hoOauth To "https://accounts.google.com/o/oauth2/v2/auth"
    Set ComLocalHost Of hoOauth To "127.0.0.1"
    Set ComListenPort Of hoOauth To 55568
    Set ComListenPortRangeEnd Of hoOauth To 55578
    Set ComCodeChallenge Of hoOauth To True
    Set ComCodeChallengeMethod Of hoOauth To "S256"
    Set ComScope Of hoOauth To "https://www.googleapis.com/auth/gmail.readonly"
    Get ComAddAuthQueryParam Of hoOauth "access_type" "offline" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not configure offline access."
        Procedure_Return
    End

    //  This is the interactive setup step. Subsequent refreshes use the saved refresh token.
    Get ComAddAuthQueryParam Of hoOauth "prompt" "consent" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not configure consent."
        Procedure_Return
    End

    Get ComStartAuth Of hoOauth To sAuthUrl
    Get ComLastMethodSuccess Of hoOauth To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Could not start the loopback listener."
        Procedure_Return
    End

    Get ComLaunchBrowser Of hoOauth sAuthUrl To iSuccess
    If (iSuccess <> True) Begin
        Get ComCancel Of hoOauth To iSuccess
        Showln "Could not open the system browser."
        Procedure_Return
    End

    Move 0 To iWaited
    While (((ComAuthFlowState(hoOauth)) < 3) And (iWaited < 180000))
        Send ComSleepMs To hoOauth 200
        Move (iWaited + 200) To iWaited
    Loop

    Get ComAuthFlowState Of hoOauth To iTemp1
    If (iTemp1 < 3) Begin
        Get ComCancel Of hoOauth To iSuccess
        Showln "Authorization timed out."
        Procedure_Return
    End

    Get ComAuthFlowState Of hoOauth To iTemp1
    If (iTemp1 <> 3) Begin
        Get ComAuthFlowState Of hoOauth To iTemp1
        Showln "Authorization did not complete. Flow state: " iTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Get ComAccessTokenResponse Of hoOauth To sTemp1
    Get ComLoad Of hoToken sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not parse the token response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoToken "refresh_token" To iTemp1
    Get ComStringOfEquals Of hoToken "refresh_token" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "No offline refresh token was issued. The existing cache was preserved."
        Procedure_Return
    End

    Get ComWriteFile Of hoToken "gmail-token.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not save the token file."
        Procedure_Return
    End

    Showln "Credentials saved in the private working directory."


End_Procedure