display the resulting json
This commit is contained in:
@ -1,15 +1,22 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Json;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Yavsc.Helpers;
|
using Yavsc.Helpers;
|
||||||
|
|
||||||
namespace testOauthClient.Controllers
|
namespace testOauthClient.Controllers
|
||||||
{
|
{
|
||||||
public class HomeController : Controller
|
public class HomeController : Controller
|
||||||
{
|
{
|
||||||
|
ILogger _logger;
|
||||||
|
public HomeController(ILoggerFactory loggerFactory)
|
||||||
|
{
|
||||||
|
_logger=loggerFactory.CreateLogger<HomeController>();
|
||||||
|
}
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IActionResult Index()
|
public IActionResult Index()
|
||||||
{
|
{
|
||||||
@ -18,10 +25,11 @@ namespace testOauthClient.Controllers
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
|
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
using (var client = new HttpClient()) {
|
using (var client = new HttpClient())
|
||||||
|
{
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, "http://dev.pschneider.fr/api/me");
|
var request = new HttpRequestMessage(HttpMethod.Get, "http://dev.pschneider.fr/api/me");
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
||||||
|
|
||||||
|
|
||||||
var response = await client.SendAsync(request, cancellationToken);
|
var response = await client.SendAsync(request, cancellationToken);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
@ -30,38 +38,55 @@ namespace testOauthClient.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> PostDeviceInfo(CancellationToken cancellationToken)
|
public async Task<IActionResult> PostDeviceInfo(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
/* using (var client = new HttpClient()) {
|
/*
|
||||||
var request = new HttpRequestMessage(HttpMethod.Post, "http://dev.pschneider.fr/api/gcm/register");
|
using (var client = new HttpClient()) {
|
||||||
|
var request = new HttpRequestMessage(HttpMethod.Post, "http://dev.pschneider.fr/api/gcm/register");
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
|
||||||
var json = JsonConvert.
|
|
||||||
SerializeObject(new Yavsc.Models.Identity.GoogleCloudMobileDeclaration { DeviceId= "devid01", GCMRegistrationId = "1234" } );
|
|
||||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
||||||
var response = await client.SendAsync(request, cancellationToken);
|
|
||||||
response.EnsureSuccessStatusCode();
|
|
||||||
|
|
||||||
return View("Index", model: await response.Content.ReadAsStringAsync());
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
|
||||||
}*/
|
var json = JsonConvert.
|
||||||
var res = await new SimpleJsonPostMethod(
|
SerializeObject(new Yavsc.Models.Identity.GoogleCloudMobileDeclaration { DeviceId= "devid01", GCMRegistrationId = "1234" } );
|
||||||
"http://dev.pschneider.fr/api/gcm/register",
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||||
"Authorization: Bearer "+AccessToken).InvokeJson( new {
|
var response = await client.SendAsync(request, cancellationToken);
|
||||||
GCMRegistrationId = "testGoogleRegistrationIdValue",
|
response.EnsureSuccessStatusCode();
|
||||||
DeviceId = "TestDeviceId",
|
|
||||||
Model = "TestModel",
|
return View("Index", model: await response.Content.ReadAsStringAsync());
|
||||||
Platform = "External Web",
|
}*/
|
||||||
Version = "0.0.1-rc1"
|
JsonValue result = null;
|
||||||
} );
|
var authHeader = $"Bearer {AccessToken}";
|
||||||
return Json(res);
|
_logger.LogWarning($"using authorization Header {authHeader}");
|
||||||
|
try {
|
||||||
|
|
||||||
|
|
||||||
|
using (var request = new SimpleJsonPostMethod(
|
||||||
|
"http://dev.pschneider.fr/api/gcm/register", authHeader))
|
||||||
|
{
|
||||||
|
result = await request.InvokeJson(new
|
||||||
|
{
|
||||||
|
GCMRegistrationId = "testGoogleRegistrationIdValue",
|
||||||
|
DeviceId = "TestDeviceId",
|
||||||
|
Model = "TestModel",
|
||||||
|
Platform = "External Web",
|
||||||
|
Version = "0.0.1-rc1"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
return View("Index", model: new { error = ex.Message });
|
||||||
|
}
|
||||||
|
return View("Index", model: result?.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected string AccessToken {
|
protected string AccessToken
|
||||||
get {
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
var claim = HttpContext.User?.FindFirst("access_token");
|
var claim = HttpContext.User?.FindFirst("access_token");
|
||||||
if (claim == null) {
|
if (claim == null)
|
||||||
|
{
|
||||||
throw new InvalidOperationException("no access_token");
|
throw new InvalidOperationException("no access_token");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user