123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.IO;
- using FastReport.Export;
- using FastReport.Cloud.OAuth;
- using FastReport.Utils;
- namespace FastReport.Cloud.StorageClient.SkyDrive
- {
- /// <summary>
- /// SkyDrive cloud storage client.
- /// </summary>
- public class SkyDriveStorageClient : CloudStorageClient
- {
- #region Fields
- private ClientInfo clientInfo;
- private string authCode;
- private string accessToken;
- #endregion // Fields
- #region Properties
- /// <summary>
- /// Gets or sets the client info.
- /// </summary>
- public ClientInfo ClientInfo
- {
- get { return clientInfo; }
- set { clientInfo = value; }
- }
- /// <summary>
- /// Gets or sets the authorization code.
- /// </summary>
- public string AuthCode
- {
- get { return authCode; }
- set { authCode = value; }
- }
- /// <summary>
- /// Gets or sets the access token.
- /// </summary>
- public string AccessToken
- {
- get { return accessToken; }
- set { accessToken = value; }
- }
- #endregion // Properties
- #region Constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="SkyDriveStorageClient"/> class.
- /// </summary>
- public SkyDriveStorageClient() : base()
- {
- clientInfo = new ClientInfo("", "", "");
- authCode = "";
- accessToken = "";
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="SkyDriveStorageClient"/> class.
- /// </summary>
- /// <param name="clientInfo">The client info.</param>
- public SkyDriveStorageClient(ClientInfo clientInfo) : base()
- {
- this.clientInfo = clientInfo;
- authCode = "";
- accessToken = "";
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="SkyDriveStorageClient"/> class.
- /// </summary>
- /// <param name="clientId">Client ID.</param>
- /// <param name="clientSecret">Client Secret.</param>
- public SkyDriveStorageClient(string clientId, string clientSecret) : base()
- {
- this.clientInfo = new ClientInfo("", clientId, clientSecret);
- authCode = "";
- accessToken = "";
- }
- #endregion // Constructors
- #region Private Methods
- private byte[] BuildGetAccessTokenRequestContent()
- {
- Dictionary<string, string> data = new Dictionary<string, string>();
- data.Add("client_id", clientInfo.Id);
- data.Add("redirect_uri", @"https://login.microsoftonline.com/common/oauth2/nativeclient");
- data.Add("grant_type", "authorization_code");
- data.Add("code", authCode);
- return Encoding.UTF8.GetBytes(HttpUtils.UrlDataEncode(data));
- }
- #endregion // Private Methods
- #region Protected Methods
- /// <inheritdoc/>
- protected override void SaveMemoryStream(MemoryStream ms)
- {
- string url = String.Format(@"https://graph.microsoft.com/v1.0/me/drive/items/root:/{0}:/content", Filename);
- try
- {
- System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
- WebRequest request = WebRequest.Create(url);
- request.Method = HttpMethod.Put;
- RequestUtils.SetProxySettings(request, ProxySettings);
- request.Headers.Add("Authorization", $"Bearer {accessToken}");
- int length = Convert.ToInt32(ms.Length);
- byte[] buffer = new byte[length];
- ms.Read(buffer, 0, length);
- request.ContentLength = buffer.Length;
- request.ContentType = "application/octet-stream";
- using (Stream rs = request.GetRequestStream())
- {
- rs.Write(buffer, 0, length);
- }
- WebResponse response = request.GetResponse();
- Stream stream = response.GetResponseStream();
- }
- catch (Exception ex)
- {
- throw new CloudStorageException(ex.Message, ex);
- }
- }
- #endregion // Protected Methods
- #region Public Methods
- /// <summary>
- /// Gets the authorization URL.
- /// </summary>
- /// <returns>The authorization URL string.</returns>
- public string GetAuthorizationUrl()
- {
- 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");
- }
- /// <summary>
- /// Gets the access token.
- /// </summary>
- /// <returns>The access token value.</returns>
- public string GetAccessToken()
- {
- System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00);
- WebRequest request = WebRequest.Create(@"https://login.microsoftonline.com/common/oauth2/v2.0/token");
- request.Method = HttpMethod.Post;
- RequestUtils.SetProxySettings(request, ProxySettings);
- request.ContentType = "application/x-www-form-urlencoded";
- byte[] content = BuildGetAccessTokenRequestContent();
- request.ContentLength = content.Length;
- using (Stream rs = request.GetRequestStream())
- {
- rs.Write(content, 0, content.Length);
- }
- using (WebResponse response = request.GetResponse())
- {
- accessToken = Parser.ParseSkyDriveToken(response.GetResponseStream());
- }
- return accessToken;
- }
- #endregion // Public Methods
- }
- }
|