-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
121 lines (102 loc) · 4.34 KB
/
MainWindow.xaml.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Navigation;
using Newtonsoft.Json.Linq;
namespace labRSOI
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
webBrowser.MessageHook += webBrowser_MessageHook;
}
IntPtr webBrowser_MessageHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//msg = 130 is the last call for when the window gets closed on a window.close() in javascript
if (msg == 130)
{
this.Close();
}
return IntPtr.Zero;
}
private void Button_Click_OAuth(object sender, RoutedEventArgs e)
{
var data = new StringBuilder();
data.Append("response_type=" + Uri.EscapeDataString("code"));
data.Append("&redirect_uri=" + Uri.EscapeDataString(RedirectUri));
data.Append("&client_id=" + Uri.EscapeDataString(ClientId));
data.Append("&scope=" + Uri.EscapeDataString("read_public, write_public"));
data.Append("&state=" + Uri.EscapeDataString("768uyFys"));
webBrowser.Navigate(MainUrl+OauthUrl+data);
}
private void OAuth(string code)
{
var data = new StringBuilder();
data.Append("grant_type=" + Uri.EscapeDataString("authorization_code"));
data.Append("&client_id=" + Uri.EscapeDataString(ClientId));
data.Append("&client_secret=" +
Uri.EscapeDataString("8f9aa54c392d6e2cb46506521509af7b953055c243cf84817b3811830d144fcc"));
data.Append("&code=" + Uri.EscapeDataString(code));
var returnVal = DoRequest(MainUrl + AccessTokenUrl + data, PostStr);
TextBlock.Text += returnVal + "\n";
var o = JObject.Parse(returnVal);
_accessToken = o["access_token"].ToString();
}
private void Button_Click_Anonim(object sender, RoutedEventArgs e)
{
var strRequest = string.Format(MainUrl +PinDetailsUrl, _accessToken);
var returnVal = DoRequest(strRequest, GetStr);
TextBlock.Text += returnVal + "\n";
var o = JObject.Parse(returnVal);
webBrowser.Navigate(o["data"]["url"].ToString());
}
private string DoRequest(string strRequest, string typeRequest)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(strRequest);
request.Method = typeRequest;
var response = request.GetResponse();
var dataStream = response.GetResponseStream();
if (dataStream != null)
{
var responseReader = new StreamReader(dataStream);
var returnVal = responseReader.ReadToEnd();
responseReader.Close();
dataStream.Close();
response.Close();
return returnVal;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message+"\n");
}
return "";
}
private void webBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.ToString().Contains("&code"))
{
var query = e.Uri.Query;
OAuth(query.Substring(query.IndexOf("&code") + 6));
}
}
private const string MainUrl = "https://api.pinterest.com/";
private const string PinDetailsUrl = "v1/pins/464293042808480392/?access_token={0}";
private const string OauthUrl = "oauth/?";
private const string AccessTokenUrl = "v1/oauth/token?";
private const string ClientId = "4802970826135581941";
private const string RedirectUri = "https://mywebsite.com/connect/pinterest/";
private const string GetStr = "GET";
private const string PostStr = "POST";
private string _accessToken = "";
}
}