-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstallController.cs
More file actions
executable file
·128 lines (109 loc) · 4.77 KB
/
InstallController.cs
File metadata and controls
executable file
·128 lines (109 loc) · 4.77 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace WebApplication2.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class InstallController : ControllerBase
{
private void Log(string msg)
{
using (StreamWriter sw = System.IO.File.AppendText("log.txt"))
{
sw.WriteLine(String.Format("{0}: {1}", DateTime.Now, msg));
}
}
// GET api/install/code
[HttpGet()]
public ActionResult<string> Get(string code)
{
string oAuthAccessToken = GetOAuthAccessToken(code);
string apiAccessToken = GetApiAccessToken(oAuthAccessToken);
string text = GetEshopInfo(apiAccessToken);
return text;
}
private string GetOAuthAccessToken(string code)
{
Log(String.Format("Installation with code '{0}'", code));
using (WebClient client = new WebClient())
{
try
{
var postData = new System.Collections.Specialized.NameValueCollection();
postData.Add("redirect_uri", "https://webinstallapplication.azurewebsites.net/api/install");
postData.Add("client_id", "3owcl3g1imxdltpp");
postData.Add("client_secret", "shoasecret_dd37b1fc0504b16308115c9e270a785058cce61b067fde960bc13db37cc66674");
postData.Add("code", code);
postData.Add("scope", "api");
postData.Add("grant_type", "authorization_code");
byte[] responsebytes = client.UploadValues("https://eliska.myshoptet.com/action/ApiOAuthServer/token", "POST", postData);
string responsebody = Encoding.UTF8.GetString(responsebytes);
Log(Response.StatusCode + ": " + responsebody);
JObject json = JObject.Parse(responsebody);
string oAuthAccessToken = (string)json["access_token"];
Log("Your OAuth Access Token: " + oAuthAccessToken);
return oAuthAccessToken;
}
catch (WebException e)
{
var response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
Log(String.Format("WebException {0}: {1}", e.Message, response));
}
return "n/a";
}
}
private string GetApiAccessToken(string oAuthAccessToken)
{
Log("GetApiAccessToken");
using (WebClient client = new WebClient())
{
try
{
client.Headers.Add("Authorization", "Bearer " + oAuthAccessToken);
string response = client.DownloadString("https://eliska.myshoptet.com/action/ApiOAuthServer/getAccessToken");
Log(response);
JObject json = JObject.Parse(response);
string apiAccessToken = (string)json["access_token"];
Log("Your API Access Token: " + apiAccessToken);
return apiAccessToken;
}
catch (WebException e)
{
var response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
Log(String.Format("WebException {0}: {1}", e.Message, response));
}
return "n/a";
}
}
private string GetEshopInfo(string apiAccessToken)
{
Log("GetEshopInfo");
string msg = "";
using (WebClient client = new WebClient())
{
try
{
client.Headers.Add("Shoptet-Access-Token", apiAccessToken);
client.Headers.Add("Content-type", "application/json");
msg = client.DownloadString("https://api.myshoptet.com/api/eshop");
JObject json = JObject.Parse(msg);
string eshopId = (string)json["data"]["contactInformation"]["eshopId"];
string url = (string)json["data"]["contactInformation"]["url"];
string name = (string)json["data"]["contactInformation"]["eshopName"];
msg += String.Format("\nEshopId: {0} Url: {1} Název: {2}", eshopId, url, name);
}
catch (WebException e)
{
var response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
msg = String.Format("WebException {0}: {1}", e.Message, response);
}
}
Log(msg);
return msg;
}
}
}