コード例 #1
0
        /// <summary>
        /// Refresh expired access token
        /// </summary>
        /// <param name="token">Access token</param>
        /// <returns>Refreshed access token</returns>
        public static ICloudStorageAccessToken RefreshToken(ICloudStorageAccessToken token)
        {
            var sdToken = token as OAuth20Token;

            if (sdToken == null || !CanRefresh(sdToken))
            {
                throw new ArgumentException("Can not refresh given token", "token");
            }

            var query = string.Format("client_id={0}&client_secret={1}&redirect_uri={2}&grant_type=refresh_token&refresh_token={3}",
                                      sdToken.ClientID, sdToken.ClientSecret, sdToken.RedirectUri, sdToken.RefreshToken);

            var json = SkyDriveRequestHelper.PerformRequest(SkyDriveConstants.OAuth20TokenUrl, "application/x-www-form-urlencoded", "POST", query, 2);

            if (json != null)
            {
                var refreshed = OAuth20Token.FromJson(json);
                refreshed.ClientID     = sdToken.ClientID;
                refreshed.ClientSecret = sdToken.ClientSecret;
                refreshed.RedirectUri  = sdToken.RedirectUri;
                refreshed.Timestamp    = DateTime.UtcNow;
                return(refreshed);
            }

            return(token);
        }
コード例 #2
0
        public override bool CopyResource(IStorageProviderSession session, ICloudFileSystemEntry entry, ICloudDirectoryEntry copyTo)
        {
            if (entry.Name.Equals("/"))
            {
                return(false);
            }

            if (entry is ICloudDirectoryEntry)
            {
                // skydrive allowes to copy only files so we will recursively create/copy entries
                var newEntry = CreateResource(session, entry.Name, copyTo) as ICloudDirectoryEntry;
                return(newEntry != null && (entry as ICloudDirectoryEntry).Aggregate(true, (current, subEntry) => current && CopyResource(session, subEntry, newEntry)));
            }

            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var data = String.Format("{{destination: \"{0}\"}}", copyTo.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "COPY", data, false);

            if (json != null && !SkyDriveJsonParser.ContainsError(json, false))
            {
                var copyToBase = copyTo as BaseDirectoryEntry;
                if (copyToBase != null)
                {
                    copyToBase.AddChild(entry as BaseFileEntry);
                }
                return(true);
            }

            return(false);
        }
コード例 #3
0
        public override bool MoveResource(IStorageProviderSession session, ICloudFileSystemEntry entry, ICloudDirectoryEntry moveTo)
        {
            if (entry.Name.Equals("/"))
            {
                return(false);
            }

            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var data = String.Format("{{destination: \"{0}\"}}", moveTo.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "MOVE", data, false);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var parent = entry.Parent as BaseDirectoryEntry;
                if (parent != null)
                {
                    parent.RemoveChildById(entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                }
                var moveToBase = moveTo as BaseDirectoryEntry;
                if (moveToBase != null)
                {
                    moveToBase.AddChild(entry as BaseFileEntry);
                }
                return(true);
            }

            return(false);
        }
コード例 #4
0
        public override ICloudFileSystemEntry CreateResource(IStorageProviderSession session, String name, ICloudDirectoryEntry parent)
        {
            if (name.Contains("/"))
            {
                throw new SharpBoxException(SharpBoxErrorCodes.ErrorInvalidFileOrDirectoryName);
            }

            var uri =
                parent != null
                    ? String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, parent.GetPropertyValue(SkyDriveConstants.InnerIDKey))
                    : SkyDriveConstants.RootAccessUrl;

            var data  = String.Format("{{name: \"{0}\"}}", name);
            var json  = SkyDriveRequestHelper.PerformRequest(session, uri, "POST", data, false);
            var entry = SkyDriveJsonParser.ParseSingleEntry(session, json);

            var parentBase = parent as BaseDirectoryEntry;

            if (parentBase != null && entry != null)
            {
                parentBase.AddChild(entry as BaseFileEntry);
            }

            return(entry);
        }
コード例 #5
0
        private static string GetSignedUploadUrl(IStorageProviderSession session, ICloudFileSystemEntry fileSystemEntry)
        {
            var uri = String.Format("{0}/{1}/files/{2}",
                                    SkyDriveConstants.BaseAccessUrl,
                                    fileSystemEntry.Parent.GetPropertyValue(SkyDriveConstants.InnerIDKey),
                                    fileSystemEntry.Name);

            return(SkyDriveRequestHelper.SignUri(session, uri));
        }
コード例 #6
0
        public override Stream CreateDownloadStream(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            if (entry is ICloudDirectoryEntry)
            {
                throw new ArgumentException("Download operation can be perform for files only");
            }

            String uri = String.Format("{0}/{1}/content", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));

            uri = SkyDriveRequestHelper.SignUri(session, uri);
            var request  = WebRequest.Create(uri);
            var response = request.GetResponse();

            ((BaseFileEntry)entry).Length = response.ContentLength;
            return(new BaseFileEntryDownloadStream(response.GetResponseStream(), entry));
        }
コード例 #7
0
        public override bool DeleteResource(IStorageProviderSession session, ICloudFileSystemEntry entry)
        {
            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "DELETE", null, true);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var parent = entry.Parent as BaseDirectoryEntry;
                if (parent != null)
                {
                    parent.RemoveChildById(entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
                }
                return(true);
            }

            return(false);
        }
コード例 #8
0
        public override bool RenameResource(IStorageProviderSession session, ICloudFileSystemEntry entry, String newName)
        {
            if (entry.Name.Equals("/") || newName.Contains("/"))
            {
                return(false);
            }

            var uri  = String.Format("{0}/{1}", SkyDriveConstants.BaseAccessUrl, entry.GetPropertyValue(SkyDriveConstants.InnerIDKey));
            var data = String.Format("{{name: \"{0}\"}}", newName);
            var json = SkyDriveRequestHelper.PerformRequest(session, uri, "PUT", data, false);

            if (!SkyDriveJsonParser.ContainsError(json, false))
            {
                var entryBase = entry as BaseFileEntry;
                if (entryBase != null)
                {
                    entryBase.Name = newName;
                }
                return(true);
            }

            return(false);
        }
コード例 #9
0
        /// <summary>
        /// Exchange your authorization code for a valid access token
        /// </summary>
        /// <param name="clientID">ID of your app</param>
        /// <param name="clientSecret">Secret of your app</param>
        /// <param name="redirectUri">Redirect uri you used to obtained authorization code. Serves as request validator</param>
        /// <param name="authCode">Authorization code</param>
        /// <returns>Access token</returns>
        public static ICloudStorageAccessToken GetAccessToken(string clientID, string clientSecret, string redirectUri, string authCode)
        {
            if (string.IsNullOrEmpty(clientID))
            {
                throw new ArgumentNullException("clientID");
            }
            if (string.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }
            if (string.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }

            if (string.IsNullOrEmpty(redirectUri))
            {
                redirectUri = SkyDriveConstants.DefaultRedirectUri;
            }

            var query = string.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code",
                                      clientID, redirectUri, clientSecret, authCode);

            var json = SkyDriveRequestHelper.PerformRequest(SkyDriveConstants.OAuth20TokenUrl, "application/x-www-form-urlencoded", "POST", query, 2);

            if (json != null)
            {
                var token = OAuth20Token.FromJson(json);
                token.ClientID     = clientID;
                token.ClientSecret = clientSecret;
                token.RedirectUri  = redirectUri;
                token.Timestamp    = DateTime.UtcNow;
                return(token);
            }

            return(null);
        }
コード例 #10
0
        private static ICloudFileSystemEntry RequestResourseByUrl(IStorageProviderSession session, String url)
        {
            var json = SkyDriveRequestHelper.PerformRequest(session, url);

            return(SkyDriveJsonParser.ParseSingleEntry(session, json));
        }
コード例 #11
0
        private static IEnumerable <ICloudFileSystemEntry> RequestContentByUrl(IStorageProviderSession session, String url)
        {
            var json = SkyDriveRequestHelper.PerformRequest(session, url);

            return(SkyDriveJsonParser.ParseListOfEntries(session, json) ?? new List <ICloudFileSystemEntry>());
        }