A successful OAuth2 token
* TestCatalogInit.cs and TestBrands.cs: do build unit test only when TEST is defined. * Web.csproj: useless change to .Net 4.5.1 framework * GoogleController.cs: A successful OAuth2 token from Google! thanks to curl.
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
|
||||
#if TEST
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using SalesCatalog.Model;
|
||||
@ -29,3 +31,4 @@ namespace SalesCatalog.Tests
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,3 +1,5 @@
|
||||
#if TEST
|
||||
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using SalesCatalog.XmlImplementation;
|
||||
@ -104,3 +106,4 @@ namespace SalesCatalog.Tests
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -11,26 +11,34 @@ using Mono.Security.Protocol.Tls;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
using Yavsc.Model;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Yavsc.Controllers
|
||||
{
|
||||
public class TokenResult {
|
||||
public string access_token { get; set; }
|
||||
public string id_token { get; set; }
|
||||
public int expires_in { get; set; }
|
||||
public string token_type { get; set ; }
|
||||
public string refresh_token { get; set; }
|
||||
}
|
||||
|
||||
public class GoogleController : Controller
|
||||
{
|
||||
|
||||
string API_KEY="AIzaSyBV_LQHb22nGgjNvFzZwnQHjao3Q7IewRw";
|
||||
private string API_KEY="AIzaSyBV_LQHb22nGgjNvFzZwnQHjao3Q7IewRw";
|
||||
|
||||
string CLIENT_ID="325408689282-6bekh7p3guj4k0f3301a6frf025cnrk1.apps.googleusercontent.com";
|
||||
private string CLIENT_ID="325408689282-6bekh7p3guj4k0f3301a6frf025cnrk1.apps.googleusercontent.com";
|
||||
|
||||
string CLIENT_SECRET="MaxYcvJJCs2gDGvaELZbzwfL";
|
||||
private string CLIENT_SECRET="MaxYcvJJCs2gDGvaELZbzwfL";
|
||||
|
||||
string [] SCOPES = {
|
||||
"profile",
|
||||
"email"
|
||||
} ;
|
||||
|
||||
string getTokenUrl = "https://accounts.google.com/o/oauth2/token";
|
||||
// "https://www.googleapis.com/oauth2/v3/token";https://accounts.google.com/o/oauth2/token
|
||||
string getCodeUrl = "https://accounts.google.com/o/oauth2/auth";
|
||||
string tokenUri = "https://accounts.google.com/o/oauth2/token";
|
||||
string authUri = "https://accounts.google.com/o/oauth2/auth";
|
||||
|
||||
public void Login()
|
||||
{
|
||||
@ -53,7 +61,7 @@ namespace Yavsc.Controllers
|
||||
state
|
||||
);
|
||||
|
||||
WebRequest wr = WebRequest.Create(getCodeUrl+"?"+prms);
|
||||
WebRequest wr = WebRequest.Create(authUri+"?"+prms);
|
||||
|
||||
wr.Method = "GET";
|
||||
// Get the response.
|
||||
@ -69,7 +77,7 @@ namespace Yavsc.Controllers
|
||||
|
||||
}
|
||||
public void Auth() {
|
||||
string redirectUri = Request.Url.Scheme + "://" + Request.Url.Authority + "/Google/Code";
|
||||
string redirectUri = Request.Url.Scheme + "://" + Request.Url.Authority + "/Google/Auth";
|
||||
string code = Request.Params ["code"];
|
||||
string error = Request.Params ["error"];
|
||||
if (error != null) {
|
||||
@ -84,37 +92,37 @@ namespace Yavsc.Controllers
|
||||
LocalizedText.ResourceManager.GetString("invalid request state");
|
||||
return;
|
||||
}
|
||||
HttpWebRequest webreq = WebRequest.CreateHttp(getTokenUrl);
|
||||
|
||||
string postdata =
|
||||
string.Format(
|
||||
"redirect_uri={0}&client_id={1}&client_secret={2}&code={3}&grant_type=authorization_code",
|
||||
HttpUtility.UrlEncode(redirectUri),
|
||||
HttpUtility.UrlEncode(CLIENT_ID),
|
||||
HttpUtility.UrlEncode(CLIENT_SECRET),
|
||||
HttpUtility.UrlEncode(code));
|
||||
|
||||
Byte[] bytes = System.Text.Encoding.UTF8.GetBytes (postdata);
|
||||
HttpWebRequest webreq = WebRequest.CreateHttp (tokenUri);
|
||||
webreq.Method = "POST";
|
||||
webreq.Accept = "application/json";
|
||||
webreq.ContentType = "application/x-www-form-urlencoded";
|
||||
webreq.SendChunked = true;
|
||||
string postData = String.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",
|
||||
code,
|
||||
CLIENT_ID,
|
||||
CLIENT_SECRET,
|
||||
redirectUri);
|
||||
Encoding encr = new UTF8Encoding();
|
||||
Byte[] bytes = encr.GetBytes(postData);
|
||||
webreq.ContentLength = bytes.Length;
|
||||
using (Stream dataStream = webreq.GetRequestStream()) {
|
||||
dataStream.Write(bytes,0,bytes.Length);
|
||||
dataStream.Close();
|
||||
}
|
||||
try {
|
||||
WebResponse response = webreq.GetResponse();
|
||||
string resQuery = response.ResponseUri.Query;
|
||||
string cont = HttpUtility.ParseQueryString(resQuery)["continue"];
|
||||
Response.Redirect (cont);
|
||||
}
|
||||
catch (WebException wex) {
|
||||
Response.Redirect(wex.Response.ResponseUri.AbsoluteUri);
|
||||
using (Stream dataStream = webreq.GetRequestStream ()) {
|
||||
dataStream.Write (bytes, 0, bytes.Length);
|
||||
};
|
||||
|
||||
using (WebResponse response = webreq.GetResponse ()) {
|
||||
|
||||
using (Stream responseStream = response.GetResponseStream ()) {
|
||||
using (StreamReader readStream = new StreamReader (responseStream, Encoding.ASCII)) {
|
||||
string responseStr = readStream.ReadToEnd ();
|
||||
TokenResult res = JsonConvert.DeserializeObject<TokenResult>(responseStr);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Code()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
<ProjectTypeGuids>{349C5851-65DF-11DA-9384-00065B846F21};{603C0E0B-DB56-11DC-BE95-000D561079B0};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>Yavsc</RootNamespace>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@ -70,25 +70,19 @@
|
||||
<Reference Include="CodeKicker.BBCode">
|
||||
<HintPath>lib\CodeKicker.BBCode.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="Mono.Posix" />
|
||||
<Reference Include="System.ServiceModel.Web" />
|
||||
<Reference Include="System.ServiceModel.Routing" />
|
||||
<Reference Include="System.Configuration.Install" />
|
||||
<Reference Include="System.Web.Services" />
|
||||
<Reference Include="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="System.Web.WebPages.Deployment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="Microsoft.Build.Tasks.v12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
|
||||
<Reference Include="Microsoft.Build.Utilities.v12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
|
||||
<Reference Include="Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
|
||||
<Reference Include="System.Web.Http.WebHost">
|
||||
<HintPath>..\..\..\..\..\usr\lib\mono\4.5\System.Web.Http.WebHost.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Net.Http.Formatting">
|
||||
<HintPath>..\..\..\..\..\usr\lib\mono\4.5\System.Net.Http.Formatting.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Net" />
|
||||
<Reference Include="log4net">
|
||||
@ -96,6 +90,10 @@
|
||||
</Reference>
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Net.Http.WebRequest" />
|
||||
<Reference Include="System.Web.Mvc" />
|
||||
<Reference Include="System.Web.Http" />
|
||||
<Reference Include="System.Web.WebPages" />
|
||||
<Reference Include="System.Web.WebPages.Deployment" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
|
Reference in New Issue
Block a user