Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 85 additions & 84 deletions AsyncOAuth.ConsoleApp/AsyncOAuth.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,91 +1,92 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C8BF0013-AE03-48C0-A11B-6CF9F39257DE}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AsyncOAuth.ConsoleApp</RootNamespace>
<AssemblyName>AsyncOAuth.ConsoleApp</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Primitives">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Twitter.cs" />
<Compile Include="Hatena.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsyncOAuth\AsyncOAuth.csproj">
<Project>{877842ff-edae-40e7-913b-c871ad5a48cc}</Project>
<Name>AsyncOAuth</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C8BF0013-AE03-48C0-A11B-6CF9F39257DE}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AsyncOAuth.ConsoleApp</RootNamespace>
<AssemblyName>AsyncOAuth.ConsoleApp</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Threading.Tasks">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Threading.Tasks.Extensions">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.165\lib\net45\Microsoft.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.Primitives">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ETrade.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Twitter.cs" />
<Compile Include="Hatena.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsyncOAuth\AsyncOAuth.csproj">
<Project>{877842ff-edae-40e7-913b-c871ad5a48cc}</Project>
<Name>AsyncOAuth</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.13\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
-->
</Project>
93 changes: 93 additions & 0 deletions AsyncOAuth.ConsoleApp/ETrade.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace AsyncOAuth.ConsoleApp
{
// a sample of ETrade client
public class ETradeClient
{
readonly string consumerKey;
readonly string consumerSecret;
readonly AccessToken accessToken;

public ETradeClient(string consumerKey, string consumerSecret, AccessToken accessToken)
{
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.accessToken = accessToken;
}

// sample flow for ETrade authroize
public async static Task<AccessToken> AuthorizeSample(string consumerKey, string consumerSecret)
{
try
{
// create authorizer
var authorizer = new OAuthAuthorizer(consumerKey, consumerSecret);

// get request token
List<KeyValuePair<string, string>> args = new List<KeyValuePair<string, string>>();
args.Add(new KeyValuePair<string, string>("oauth_callback", "oob"));

var tokenResponse = await authorizer.GetRequestToken("https://etws.etrade.com/oauth/request_token",args);
var requestToken = tokenResponse.Token;


// var pinRequestUrl = authorizer.BuildAuthorizeUrl("https://us.etrade.com/e/t/etws/authorize", requestToken);
string pinRequestUrl = "https://us.etrade.com/e/t/etws/authorize" + "?key=" + consumerKey + "&token=" + requestToken.Key;
// open browser and get PIN Code
Process.Start(pinRequestUrl);

// enter pin
Console.WriteLine("ENTER PIN");
var pinCode = Console.ReadLine();

// get access token

var accessTokenResponse = await authorizer.GetAccessToken("https://etws.etrade.com/oauth/access_token", requestToken, pinCode);

// save access token.
var accessToken = accessTokenResponse.Token;
Console.WriteLine("Access Granted: ");
Console.WriteLine(" Access Key:" + accessToken.Key);
Console.WriteLine(" Access Secret:" + accessToken.Secret);
Console.WriteLine("===============================================");

return accessToken;
}
catch (Exception e)
{
Console.WriteLine("Exception " + e.ToString());
}

return null;
}

public async Task<string> GetQuote()
{
try
{


var client = OAuthUtility.CreateOAuthClient(consumerKey, consumerSecret, accessToken);

var json = await client.GetStringAsync("https://etwssandbox.etrade.com/market/sandbox/rest/quote/GOOG,MSFT.json?detailFlag=FUNDAMENTAL");

return json;
}
catch (Exception e )
{
Console.WriteLine("Exception in GetQuote: " + e.ToString());
}

return null;
}


}
}
39 changes: 30 additions & 9 deletions AsyncOAuth.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,49 @@
namespace AsyncOAuth.ConsoleApp
{
class Program
{
// set your token
const string consumerKey = "";
const string consumerSecret = "";
{
// set your token


const string consumerKey = "yourConsumerKey";
const string consumerSecret = "yourConsumerToken";

static async Task Run()
{
// initialize computehash function
OAuthUtility.ComputeHash = (key, buffer) => { using (var hmac = new HMACSHA1(key)) { return hmac.ComputeHash(buffer); } };

// sample, twitter access flow
var accessToken = await TwitterClient.AuthorizeSample(consumerKey, consumerSecret);
// sample, ETrade access flow
Console.WriteLine("ASyncOauth ETrade Sample: ");
System.Threading.Thread.Sleep(1000);

var client = new TwitterClient(consumerKey, consumerSecret, accessToken);
Console.WriteLine("Getting access token");
var accessToken = await ETradeClient.AuthorizeSample(consumerKey, consumerSecret);

var tl = await client.GetTimeline(10, 1);
Console.WriteLine(tl);
if ( accessToken == null )
{
Console.WriteLine("Error getting access token");
return;
}

// create the client with the access token and consumer key
var client = new ETradeClient(consumerKey, consumerSecret, accessToken);

// Get Quote:
Console.WriteLine("Getting Quotes from sandbox server: ");

var quoteResponse = await client.GetQuote();

Console.WriteLine(quoteResponse);
}

static void Main(string[] args)
{
Run().Wait();

// Exit
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
17 changes: 17 additions & 0 deletions AsyncOAuth/OAuthUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,27 @@ public static IEnumerable<KeyValuePair<string, string>> BuildBasicParameters(str
if (token != null) parameters.Add(new KeyValuePair<string, string>("oauth_token", token.Key));
if (optionalParameters == null) optionalParameters = Enumerable.Empty<KeyValuePair<string, string>>();

// generate the signature
var signature = GenerateSignature(consumerSecret, new Uri(url), method, token, parameters.Concat(optionalParameters));

// add the signature to the parameters
parameters.Add(new KeyValuePair<string, string>("oauth_signature", signature));

// TODO - this is a change made to get library to work with ETrade OAuth login
// the token must be URL encoded, but if you URL encode token prior to generate signature, signature is incorrect
// this is brute force solution that worked for me, but there is probably a better way to handle this
try
{
var findToken = parameters.First(x => x.Key == "oauth_token");

parameters.Remove(findToken);
parameters.Add(new KeyValuePair<string, string>(findToken.Key, findToken.Value.UrlEncode()));
}
catch
{
// oauth_token is not in the header, continue
}

return parameters;
}

Expand Down