TryIT.MicrosoftGraphApi 1.6.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package TryIT.MicrosoftGraphApi --version 1.6.6                
NuGet\Install-Package TryIT.MicrosoftGraphApi -Version 1.6.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TryIT.MicrosoftGraphApi" Version="1.6.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TryIT.MicrosoftGraphApi --version 1.6.6                
#r "nuget: TryIT.MicrosoftGraphApi, 1.6.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TryIT.MicrosoftGraphApi as a Cake Addin
#addin nuget:?package=TryIT.MicrosoftGraphApi&version=1.6.6

// Install TryIT.MicrosoftGraphApi as a Cake Tool
#tool nuget:?package=TryIT.MicrosoftGraphApi&version=1.6.6                

How to use this library

access https://docs.microsoft.com/en-us/graph/auth-v2-user to understand more

  1. create a page to allow manual sign in account
  2. page callback method, obtain access_token and save it in somewhere (e.g. Session / DB)
  3. use that access_token to call Graph Service

How to use TokenHelper to obtain access_token

  1. add reference to TryIT.MicrosoftGraphApi into your code
  2. create a page to allow Administrator initial token
  3. in that page, use example code below to get token and save it
// page action call this method, and this method will redirect to AD login page
private MsGraphGetTokenConfig GetTokenConfig
{
    get
    {
        return new MsGraphGetTokenConfig
        { 
            OAuth_AuthorizeUrl = "",
            OAuth_TenantId = "",
            OAuth_ClientId = "",
            OAuth_RedirectUrl = "", // this is Callback url
            OAuth_Scope = "",
            OAuth_GetTokenUrl = "",
            OAuth_ClientSecret = "",

            OAuth_IsProxyRequired = "",
            Proxy_Url = "",
            Proxy_Username = "",
            Proxy_Password = ""
        };
    }
}

public void SignIn()
{
    TokenHelper tokenHelper = new TokenHelper(GetTokenConfig);

    // 1. validate token config
    if (tokenHelper.IsOAuthParameterValid)
    {
        // if token parameter is valid, then use browser redirect to the tokenHelper.AuthorizeUrl (Callback below)
        string authorizeURL = tokenHelper.AuthorizeUrl;

        // use code to redirect to authorizeURL
        this.RedirectUrl(authorizeURL);
    }
}

// this url is configured in Azure, after sign in, will redirect back to this url
public void Callback()
{
    string code = string.Empty;
    string state = string.Empty;

    if (!string.IsNullOrEmpty(code))
    {
        GetTokenResponse tokenResponse = tokenHelper.GetToken(code, state);
        // save token if need
    }
    else
    {
        // invalid response
    }
}

private void SaveToken(GetTokenResponse token)
{
    // save token somewhere
}

private string GetToken()
{
    /*
    * 1. get exists token (StateGuid, ExpiresOn, Access_token, Refresh_token)
    * 2. check token expiry, if ExpiresOn < Now, need refresh token to get new token
    * 3. save new token
    */

    var savedToken = "" // get saved token from session / db, this could be Object or DataTable

    if("savedToken is valid" == true) // check the token validity, e.g. check is not null
    {
        GetTokenResponse token = new GetTokenResponse
        {
            state = "[get state from exists token]",
            expires_on = "[get expires_on from exists token]",
            access_token = "[get expires_on from exists token]",
            refresh_token = "[get refresh_token from exists token]"
        };

        // here check token expiry, expires_on < DateTime.Now, if expired, will call RefreshToken to get latest valid token
        if(token.expires_on < DateTime.Now)
        {
            MsGraphGetTokenConfig tokenConfig = GetTokenConfig;
            TokenHelper tokenHelper = new TokenHelper(tokenConfig);
            GetTokenResponse tokenResponse = tokenHelper.RefreshToken(token.refresh_token, token.state);

            SaveToken(tokenResponse);

            return tokenResponse.access_token;
        }

        return token.access_token;
    }
    else
    {
        throw new Exception("Token not exists, please SignIn first.");
    }
}

How to use this package to access GraphAPI

  1. add reference to TryIT.MicrosoftGraphApi into your code
  2. use following example code
MsGraphApiConfig graphConfig = new MsGraphApiConfig
{
    Proxy_Url = "[if the network restricted need proxy setup, indicator Proxy Url here]",
    Proxy_Username = "[if the network restricted need proxy setup, indicator Proxy Username here]",
    Proxy_Password = "[if the network restricted need proxy setup, indicator Proxy Password here]",
    Token = GetToken(); // "[this should be valid token, service will use this token to call Graph API]"
};

OutlookApi MsOutlook = new OutlookApi(graphConfig);
OutlookApi.GetMessages();

Note

SharepointApi Upload file method will replace #, % to _ in file name, refer to https://support.microsoft.com/en-gb/office/restrictions-and-limitations-in-onedrive-and-sharepoint-64883a5d-228e-48f5-b3d2-eb39e07630fa#invalidcharacters

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.0 47 12/17/2024
1.6.9 96 11/26/2024
1.6.8 82 11/26/2024
1.6.7 64 11/25/2024
1.6.6 85 11/23/2024
1.6.5 86 11/22/2024
1.6.4 89 11/20/2024
1.6.3 87 11/20/2024
1.6.2 82 11/14/2024
1.6.1 81 11/7/2024
1.6.0 78 11/5/2024
1.5.9 86 11/4/2024
1.5.8 80 10/25/2024
1.5.7 74 10/23/2024
1.5.6 80 10/23/2024
1.5.5 87 10/23/2024
1.5.4 80 10/20/2024
1.5.3 106 10/18/2024
1.5.2 76 10/17/2024
1.5.1 75 10/17/2024
1.5.0 74 10/17/2024
1.4.9 103 10/2/2024
1.4.8 92 10/2/2024
1.4.7 86 10/2/2024
1.4.6 97 9/20/2024
1.4.5 94 9/20/2024
1.4.4 102 9/17/2024
1.4.3 109 9/17/2024
1.4.2 106 9/17/2024
1.4.1 99 9/17/2024
1.4.0 122 8/28/2024
1.3.9 67 8/1/2024
1.3.8 98 7/16/2024
1.3.7 118 6/30/2024
1.3.6 100 6/30/2024
1.3.5 103 6/29/2024
1.3.4 103 6/29/2024
1.3.3 110 6/29/2024
1.3.2 127 3/15/2024
1.3.1 111 3/15/2024
1.3.0 119 3/15/2024
1.2.9 118 3/15/2024
1.2.8 101 3/15/2024
1.2.7 108 3/15/2024
1.2.6 122 3/15/2024
1.2.5 123 3/15/2024
1.2.4 116 3/15/2024
1.2.3 122 1/31/2024
1.2.2 99 1/31/2024
1.2.1 100 1/25/2024
1.2.0 157 12/14/2023
1.1.9 146 12/8/2023
1.1.8 151 11/28/2023
1.1.7 141 10/30/2023
1.1.6 142 10/25/2023
1.1.5 130 10/19/2023
1.1.4 130 10/18/2023
1.1.3 160 10/13/2023
1.1.2 152 10/13/2023
1.1.1 105 10/5/2023
1.1.0 124 9/21/2023
1.0.9 129 9/21/2023
1.0.8 157 9/6/2023
1.0.7 183 8/31/2023
1.0.6 157 8/31/2023
1.0.5 176 7/28/2023
1.0.4 162 7/25/2023