-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathAuthorAPI.cs
More file actions
355 lines (298 loc) · 13.2 KB
/
AuthorAPI.cs
File metadata and controls
355 lines (298 loc) · 13.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;
using System.Xml;
using System.Web;
using System.IO;
namespace WeCode1._0
{
public static class AuthorAPI
{
/// <summary>
/// 判断是否授权或者授权是否有效
/// </summary>
/// <returns>结果</returns>
public static string GetIsAuthor()
{
string result = "OK";
if (ConfigurationManager.AppSettings["AccessToken"] == "")
{
result = "FAIL0";
}
else
{
//测试获取用户信息
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/user/get.json"); //可变字符串
//追加组合格式字符串
url.AppendFormat("?oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
string Input = url.ToString();
//HttpWebRequest对象实例:该类用于获取和操作HTTP请求
var request = (HttpWebRequest)WebRequest.Create(Input); //Create:创建WebRequest对象
//设置请求方法为GET
//request.Headers.Add("Authorization", "Bearer " + accessToken);
request.Method = "GET";
//HttpWebResponse对象实例:该类用于获取和操作HTTP应答
var response = (HttpWebResponse)request.GetResponse(); //GetResponse:获取答复
//构造数据流对象实例
if (response.StatusCode.ToString() == "OK")
{
result = "OK";
}
else
{
result = "FAIL1";
}
response.Close();
}
catch (Exception msg) //异常处理
{
result = "ERROR";
}
}
return result;
}
//创建默认的wecode目录以及xml笔记
public static string CreatewecodeConfig()
{
string result = "";
//wecode笔记本路径
string wecodepath = "";
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/notebook/all.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
//获取POST提交数据返回内容
string JNotebookListAll = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
//JObject obj = JObject.Parse(AccessContent);
JArray ja = (JArray)JsonConvert.DeserializeObject(JNotebookListAll);
string isHavewecode = "false";
foreach (JObject jo in ja)
{
//如果已存在,返回笔记路径
if (jo["name"].ToString() == "wecode")
{
wecodepath = jo["path"].ToString();
isHavewecode = "true";
break;
}
}
if (isHavewecode == "false")
{
//不存在,先创建wecode目录,并返回路径,并且创建wecodenote目录
wecodepath = CreateWecodeDir();
CreateWecodenoteDir();
//创建配置文件
if (wecodepath != "")
{
result = CreateConfigNote(wecodepath);
}
}
}
catch (Exception msg) //异常处理
{
result = "";
}
}
return result;
}
//创建wecode目录
public static string CreateWecodeDir()
{
string result = "";
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/notebook/create.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
urlPoststr.AppendFormat("name={0}", "wecode");
//获取POST提交数据返回内容
string JResult = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
//JObject obj = JObject.Parse(AccessContent);
JObject obj = JObject.Parse(JResult);
result = obj["path"].ToString();
}
catch (Exception msg) //异常处理
{
result = "";
}
}
return result;
}
//创建wecodenote目录
public static string CreateWecodenoteDir()
{
string result = "";
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/notebook/create.json"); //可变字符串
//获取access_token构造POST参数
StringBuilder urlPoststr = new StringBuilder(""); // 可变字符串
//追加组合格式字符串
urlPoststr.AppendFormat("oauth_token={0}&", ConfigurationManager.AppSettings["AccessToken"]);
urlPoststr.AppendFormat("name={0}", "wecodenote");
//获取POST提交数据返回内容
string JResult = sendMessage(url.ToString(), urlPoststr.ToString());
//获取的数据装换为Json格式 此时返回的json格式的数据
//JObject obj = JObject.Parse(AccessContent);
JObject obj = JObject.Parse(JResult);
result = obj["path"].ToString();
}
catch (Exception msg) //异常处理
{
result = "";
}
}
return result;
}
//创建默认目录笔记
public static string CreateConfigNote(string wecodebookpath)
{
string result = "";
if (ConfigurationManager.AppSettings["AccessToken"] != "")
{
try
{
//获取AccessTokon构造URL
StringBuilder url = new StringBuilder("https://note.youdao.com/yws/open/note/create.json"); //可变字符串
NameValueCollection myCol = new NameValueCollection();
myCol.Add("notebook", wecodebookpath);//如果键值red相同结果合并 rojo,rouge
myCol.Add("content", getConfigtext());
myCol.Add("title", "wecodeconfig");
string JResult = HttpPostData(url.ToString(), "", myCol);
//获取的数据装换为Json格式 此时返回的json格式的数据
//JObject obj = JObject.Parse(AccessContent);
JObject obj = JObject.Parse(JResult);
result = obj["path"].ToString();
}
catch (Exception msg) //异常处理
{
result = "";
}
}
return result;
}
//获取本地xml内容,转码
public static string getConfigtext()
{
XmlDocument doc = new XmlDocument();
doc.Load("TreeNodeLocal.xml");
string inTXT = doc.InnerXml;
inTXT = HttpUtility.HtmlEncode(inTXT);
return inTXT;
}
//发送消息Post方法
public static string sendMessage(string strUrl, string PostStr)
{
try
{
//设置消息头
CookieContainer objCookieContainer = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = strUrl;
if (objCookieContainer == null)
objCookieContainer = new CookieContainer();
request.CookieContainer = objCookieContainer;
byte[] byteData = Encoding.UTF8.GetBytes(PostStr.ToString().TrimEnd('&'));
request.ContentLength = byteData.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(byteData, 0, byteData.Length);
}
//Response应答流获取数据
string strResponse = "";
using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
{
objCookieContainer = request.CookieContainer;
using (Stream resStream = res.GetResponseStream())
{
using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8)) //UTF8格式
{
strResponse = sr.ReadToEnd();
}
}
// res.Close();
}
return strResponse;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.Read();
}
return null;
}
//post数据,ContentType = "multipart/form-data"
private static string HttpPostData(string url, string filePath, NameValueCollection stringDict)
{
//字符串参数
string Pararesult = "";
string responseContent;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 设置属性
webRequest.Method = "POST";
webRequest.Headers.Add("Authorization", "OAuth oauth_token=\"" + ConfigurationManager.AppSettings["AccessToken"] + "\"");
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
//写入字符串的Key
string stringKeyHeader = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
foreach (string key in stringDict.Keys)
{
Pararesult += string.Format(stringKeyHeader, key, stringDict[key]);
}
var paraByte = Encoding.UTF8.GetBytes(Pararesult);
memStream.Write(paraByte, 0, paraByte.Length);
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),
Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
}
httpWebResponse.Close();
webRequest.Abort();
return responseContent;
}
}
}