SkyDriveStorageClient.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. using FastReport.Export;
  7. using FastReport.Cloud.OAuth;
  8. using FastReport.Utils;
  9. namespace FastReport.Cloud.StorageClient.SkyDrive
  10. {
  11. /// <summary>
  12. /// SkyDrive cloud storage client.
  13. /// </summary>
  14. public class SkyDriveStorageClient : CloudStorageClient
  15. {
  16. #region Fields
  17. private ClientInfo clientInfo;
  18. private string authCode;
  19. private string accessToken;
  20. #endregion // Fields
  21. #region Properties
  22. /// <summary>
  23. /// Gets or sets the client info.
  24. /// </summary>
  25. public ClientInfo ClientInfo
  26. {
  27. get { return clientInfo; }
  28. set { clientInfo = value; }
  29. }
  30. /// <summary>
  31. /// Gets or sets the authorization code.
  32. /// </summary>
  33. public string AuthCode
  34. {
  35. get { return authCode; }
  36. set { authCode = value; }
  37. }
  38. /// <summary>
  39. /// Gets or sets the access token.
  40. /// </summary>
  41. public string AccessToken
  42. {
  43. get { return accessToken; }
  44. set { accessToken = value; }
  45. }
  46. #endregion // Properties
  47. #region Constructors
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="SkyDriveStorageClient"/> class.
  50. /// </summary>
  51. public SkyDriveStorageClient() : base()
  52. {
  53. clientInfo = new ClientInfo("", "", "");
  54. authCode = "";
  55. accessToken = "";
  56. }
  57. /// <summary>
  58. /// Initializes a new instance of the <see cref="SkyDriveStorageClient"/> class.
  59. /// </summary>
  60. /// <param name="clientInfo">The client info.</param>
  61. public SkyDriveStorageClient(ClientInfo clientInfo) : base()
  62. {
  63. this.clientInfo = clientInfo;
  64. authCode = "";
  65. accessToken = "";
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="SkyDriveStorageClient"/> class.
  69. /// </summary>
  70. /// <param name="clientId">Client ID.</param>
  71. /// <param name="clientSecret">Client Secret.</param>
  72. public SkyDriveStorageClient(string clientId, string clientSecret) : base()
  73. {
  74. this.clientInfo = new ClientInfo("", clientId, clientSecret);
  75. authCode = "";
  76. accessToken = "";
  77. }
  78. #endregion // Constructors
  79. #region Private Methods
  80. private byte[] BuildGetAccessTokenRequestContent()
  81. {
  82. Dictionary<string, string> data = new Dictionary<string, string>();
  83. data.Add("client_id", clientInfo.Id);
  84. data.Add("redirect_uri", @"https://login.microsoftonline.com/common/oauth2/nativeclient");
  85. data.Add("grant_type", "authorization_code");
  86. data.Add("code", authCode);
  87. return Encoding.UTF8.GetBytes(HttpUtils.UrlDataEncode(data));
  88. }
  89. #endregion // Private Methods
  90. #region Protected Methods
  91. /// <inheritdoc/>
  92. protected override void SaveMemoryStream(MemoryStream ms)
  93. {
  94. string url = String.Format(@"https://graph.microsoft.com/v1.0/me/drive/items/root:/{0}:/content", Filename);
  95. try
  96. {
  97. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  98. WebRequest request = WebRequest.Create(url);
  99. request.Method = HttpMethod.Put;
  100. RequestUtils.SetProxySettings(request, ProxySettings);
  101. request.Headers.Add("Authorization", $"Bearer {accessToken}");
  102. int length = Convert.ToInt32(ms.Length);
  103. byte[] buffer = new byte[length];
  104. ms.Read(buffer, 0, length);
  105. request.ContentLength = buffer.Length;
  106. request.ContentType = "application/octet-stream";
  107. using (Stream rs = request.GetRequestStream())
  108. {
  109. rs.Write(buffer, 0, length);
  110. }
  111. WebResponse response = request.GetResponse();
  112. Stream stream = response.GetResponseStream();
  113. }
  114. catch (Exception ex)
  115. {
  116. throw new CloudStorageException(ex.Message, ex);
  117. }
  118. }
  119. #endregion // Protected Methods
  120. #region Public Methods
  121. /// <summary>
  122. /// Gets the authorization URL.
  123. /// </summary>
  124. /// <returns>The authorization URL string.</returns>
  125. public string GetAuthorizationUrl()
  126. {
  127. return String.Format($@"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&client_id={clientInfo.Id}&scope=files.readwrite offline_access&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient");
  128. }
  129. /// <summary>
  130. /// Gets the access token.
  131. /// </summary>
  132. /// <returns>The access token value.</returns>
  133. public string GetAccessToken()
  134. {
  135. System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
  136. WebRequest request = WebRequest.Create(@"https://login.microsoftonline.com/common/oauth2/v2.0/token");
  137. request.Method = HttpMethod.Post;
  138. RequestUtils.SetProxySettings(request, ProxySettings);
  139. request.ContentType = "application/x-www-form-urlencoded";
  140. byte[] content = BuildGetAccessTokenRequestContent();
  141. request.ContentLength = content.Length;
  142. using (Stream rs = request.GetRequestStream())
  143. {
  144. rs.Write(content, 0, content.Length);
  145. }
  146. using (WebResponse response = request.GetResponse())
  147. {
  148. accessToken = Parser.ParseSkyDriveToken(response.GetResponseStream());
  149. }
  150. return accessToken;
  151. }
  152. #endregion // Public Methods
  153. }
  154. }