This repository has been archived by the owner on Dec 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuthServer.cs
103 lines (90 loc) · 2.88 KB
/
OAuthServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Collections.Specialized;
using Extensions;
using System.IO;
using Newtonsoft.Json;
using System.Threading;
namespace helloJkw
{
public static class OAuthServer
{
static string clientId = "CLIENT_ID";
static string clientSecret = "CLIENT_SECRET";
static string apiKey = "API_KEY";
static OAuthServer()
{
var filepath = @"jkw/db/googleHelloJkwOAuthInfo.txt";
var json = File.ReadAllText(filepath, Encoding.UTF8);
dynamic OAuthInfo = JsonConvert.DeserializeObject(json);
clientId = OAuthInfo.clientId;
clientSecret = OAuthInfo.clientSecret;
apiKey = OAuthInfo.apiKey;
//clientId.Dump();
//clientSecret.Dump();
//apiKey.Dump();
}
public static async Task<string> GetAccessTokenAsync(string code, string redirect, string siteBase)
{
string json = string.Empty;
siteBase = siteBase.Contains("hellojkw.com") ? siteBase.Replace("http://", "https://") : siteBase;
try
{
var url = "https://www.googleapis.com/oauth2/v3/token";
var data = new Dictionary<string, string>();
data["code"] = code;
data["client_id"] = clientId;
data["client_secret"] = clientSecret;
data["redirect_uri"] = "{0}/oauth/{1}".With(siteBase, redirect);
data["grant_type"] = "authorization_code";
var param = data.Select(e => "{0}={1}".With(e.Key, e.Value)).StringJoin("&");
var paramBytes = Encoding.ASCII.GetBytes(param);
var request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//request.Host = "www.googleapis.com";
request.ContentLength = paramBytes.Length;
using (var stream = await request.GetRequestStreamAsync())
{
stream.Write(paramBytes, 0, paramBytes.Length);
}
var response = (HttpWebResponse)await request.GetResponseAsync();
var responseString = await new StreamReader(response.GetResponseStream()).ReadToEndAsync();
json = responseString;
}
catch (Exception ex)
{
Logger.Log(ex);
}
return json;
}
public static async Task<string> GetAccountInfoAsync(string access_token)
{
string json = string.Empty;
try
{
var url = "https://www.googleapis.com/plus/v1/people/me";
var request = WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//request.Host = "www.googleapis.com";
request.ContentLength = 0;
request.Headers.Add("Authorization", "Bearer " + access_token);
request.Headers.Add("key", apiKey);
var response = (HttpWebResponse) await request.GetResponseAsync();
json = await new StreamReader(response.GetResponseStream()).ReadToEndAsync();
Logger.Log(json);
}
catch (Exception ex)
{
Logger.Log(ex);
return json;
}
return json;
}
}
}