Sample code for 30+ languages & platforms
DataFlex

SharePoint Online Authentication

Demonstrates how to authenticate with SharePoint Online. The end result of authentication is to establish an HTTP cookie, named "SPOIDCRL", that contains a binary security token to be sent in subsequent SharePoint HTTPS requests.

The SPOIDCRL cookie can be persisted to a file and re-used in subsequent application runs.

Important: This example uses a method, SharePointOnlineAuth, that is introduced in Chilkat v9.5.0.73.

Also Important: Chilkat realizes there may be cases where your situation is such that it does not work, and something additional is required to make it work. If the example does not work out-of-the-box, please send email to support@chilkatsoft.com. The information logged to the LastErrorText property has been carefully crafted to help solve problems quickly. Also, the JSON argument to the SharePointOnlineAuth method provides a means to include additional information, whatever it may be, to handle future unknown situations.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sSiteUrl
    String sUsername
    Variant vJsonExtra
    Handle hoJsonExtra
    Variant vSsPassword
    Handle hoSsPassword
    Variant vSbResponseXml
    Handle hoSbResponseXml
    Handle hoXml
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Change these things...
    // Make sure to use "https" and end in a "/".
    Move "https://mydomain.sharepoint.com/" To sSiteUrl
    // The username is an email address.
    Move "username@mydomain.com" To sUsername

    // The jsonExtra is for future use, if extra information is required for particular integrations.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonExtra
    If (Not(IsComObjectCreated(hoJsonExtra))) Begin
        Send CreateComObject of hoJsonExtra
    End

    Get Create (RefClass(cComChilkatSecureString)) To hoSsPassword
    If (Not(IsComObjectCreated(hoSsPassword))) Begin
        Send CreateComObject of hoSsPassword
    End
    Get ComAppend Of hoSsPassword "mypassword" To iSuccess

    // Set properties to save/send cookies, and specify a cookie directory.
    // If the CookieDir = "memory", then the SPOIDCRL cookie is persisted in memory for this HTTP object instance only.
    // To persist the SharePoint authentication cookie for other HTTP objects, and for future application runs,
    // set the CookieDir equal to a directory path (not a specific file path, but a directory path where cookie files
    // are to be created).
    Set ComSaveCookies Of hoHttp To True
    Set ComSendCookies Of hoHttp To True
    Set ComCookieDir Of hoHttp To "memory"

    // The SharePointOnlineAuth method is introduced in Chilkat v9.5.0.73
    Get pvComObject of hoSsPassword to vSsPassword
    Get pvComObject of hoJsonExtra to vJsonExtra
    Get ComSharePointOnlineAuth Of hoHttp sSiteUrl sUsername vSsPassword vJsonExtra To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Success! We have the SPOIDCRL cookie..."

    // -----------------------------------------------------------------------------------------------------------
    // IMPORTANT:
    // The purpose of the SharePointOnlineAuth method is to establish the SPOIDCRL cookie that will automatically be
    // included in subsequent requests using the *** same HTTP object instance. ***
    // If later you create a new HTTP object instance, then you'll need to re-establish the SPOIDCRL cookie before sending 
    // a Sharepoint HTTP request.
    // -----------------------------------------------------------------------------------------------------------

    // SharePoint authenticated requests may now be sent because the authentication cookie is automatically included.
    // For example:
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseXml
    If (Not(IsComObjectCreated(hoSbResponseXml))) Begin
        Send CreateComObject of hoSbResponseXml
    End
    // If your Sharepoint site is within a site collection, then use "https://mydomain.sharepoint.com/sites/teamA/_api/web/GetFolderByServerRelativeUrl('/sites/teamA/Documents')/Files" where "teamA" is the name of the site.
    Get pvComObject of hoSbResponseXml to vSbResponseXml
    Get ComQuickGetSb Of hoHttp "https://mydomain.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files" vSbResponseXml To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLastStatus Of hoHttp To iTemp1
    Showln "Response status code = " iTemp1

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get pvComObject of hoSbResponseXml to vSbResponseXml
    Get ComLoadSb Of hoXml vSbResponseXml True To iSuccess
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1


End_Procedure