Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Adding Context Switching support for OAuth2
Browse files Browse the repository at this point in the history
Adding Context Switching support for OAuth2 authentication.
  • Loading branch information
manivinesh authored and sfdrogojan committed May 15, 2019
1 parent 9bf0a99 commit 38dde7c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
22 changes: 19 additions & 3 deletions FuelSDK-CSharp/ETClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace FuelSDK
/// </summary>
public class ETClient
{
public const string SDKVersion = "FuelSDK-C#-v1.2.0";
public const string SDKVersion = "FuelSDK-C#-v1.2.1";

private FuelSDKConfigurationSection configSection;
public string AuthToken { get; private set; }
Expand Down Expand Up @@ -102,6 +102,14 @@ public ETClient(NameValueCollection parameters = null, RefreshState refreshState
{
configSection.UseOAuth2Authentication = parameters["useOAuth2Authentication"];
}
if (parameters.AllKeys.Contains("accountId"))
{
configSection.AccountId = parameters["accountId"];
}
if (parameters.AllKeys.Contains("scope"))
{
configSection.Scope = parameters["scope"];
}
}
if (string.IsNullOrEmpty(configSection.ClientId) || string.IsNullOrEmpty(configSection.ClientSecret))
throw new Exception("clientId or clientSecret is null: Must be provided in config file or passed when instantiating ETClient");
Expand Down Expand Up @@ -338,8 +346,16 @@ private void RefreshTokenWithOauth2(bool force = false)

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = string.Format("{{\"client_id\": \"{0}\", \"client_secret\": \"{1}\", \"grant_type\": \"client_credentials\"}}", configSection.ClientId, configSection.ClientSecret);
streamWriter.Write(json);
dynamic payload = new JObject();
payload.client_id = configSection.ClientId;
payload.client_secret = configSection.ClientSecret;
payload.grant_type = "client_credentials";
if (!string.IsNullOrEmpty(configSection.AccountId))
payload.account_id = configSection.AccountId;
if (!string.IsNullOrEmpty(configSection.Scope))
payload.scope = configSection.Scope;

streamWriter.Write(payload.ToString());
}

// Get the response
Expand Down
1 change: 1 addition & 0 deletions FuelSDK-CSharp/FuelSDK-CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<AssemblyOriginatorKeyFile>FuelSDKKeyFile.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down
22 changes: 22 additions & 0 deletions FuelSDK-CSharp/FuelSDKConfigurationSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ public string UseOAuth2Authentication
get { return (string)this["useOAuth2Authentication"]; }
set { this["useOAuth2Authentication"] = value; }
}

/// <summary>
/// Gets or sets the Account Id.
/// </summary>
/// <value>Authenticaton Mode</value>
[ConfigurationProperty("accountId", DefaultValue = "")]
public string AccountId
{
get { return (string)this["accountId"]; }
set { this["accountId"] = value; }
}
/// <summary>
/// Gets or sets the Authenticaton Mode.
/// </summary>
/// <value>Authenticaton Mode</value>
[ConfigurationProperty("scope", DefaultValue = "")]
public string Scope
{
get { return (string)this["scope"]; }
set { this["scope"] = value; }
}

/// <summary>
/// Clone this instance.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ For Legacy authentication, use the below example for `App.config`
</configuration>
```

For OAuth2 authentication, use the below example for `App.config`
For OAuth2 authentication [More Details][here](https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/access-token-s2s.htm)., use the below example for `App.config`
```
<?xml version="1.0"?>
<configuration>
Expand All @@ -60,7 +60,9 @@ For OAuth2 authentication, use the below example for `App.config`
clientSecret="YOUR_CLIENT_SECRET"
authEndPoint="YOUR_AUTH_TSE"
restEndPoint="YOUR_REST_TSE"
useOAuth2Authentication="true" />
useOAuth2Authentication="true"
accountId="TARGET_ACCOUNT_ID"
scope="DATA_ACCESS_PERMISSIONS" />
</configuration>
```

Expand Down
2 changes: 1 addition & 1 deletion nuspecs/FuelSDK-CSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package>
<metadata>
<id>SFMC.FuelSDK</id>
<version>1.2.0</version>
<version>1.2.1</version>
<title>FuelSDK-CSharp</title>
<authors>Salesforce</authors>
<owners>Salesforce</owners>
Expand Down

0 comments on commit 38dde7c

Please sign in to comment.