using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks;
if (code != null) { //填寫post需要的line login參數 var dict = new Dictionary<string, string>(); dict.Add("grant_type", "authorization_code"); dict.Add("code", code); dict.Add("redirect_uri", "http://localhost:5000/index.html"); dict.Add("client_id", "yourid"); dict.Add("client_secret", "yourkey");
//執行post using (var client = new HttpClient()) { //注意utf8防止亂碼 client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
var req = new HttpRequestMessage( HttpMethod.Post, @"https://api.line.me/oauth2/v2.1/token") { Content = new FormUrlEncodedContent(dict) }; var res = await client.SendAsync(req); var json = await res.Content.ReadAsStreamAsync(); LineCallbackResult callbackResult = await JsonSerializer.DeserializeAsync<LineCallbackResult>(json);