Example #1
0
        private async void StreamThread()
        {
            foreach (var item in token)
            {
                Authentication.TokenResult appToken = null;
                if (item.Token.Drive == "Google")
                {
                    appToken = await WebAuthorization.RedeemAuthorizationCodeAsync(Client.GoogleClientID, UriCollection.GoogleinstalledAppRedirectUri, Client.GoogleClientSecret,
                                                                                   item.Token.refresh_token, UriCollection.GoogleTokenUri, "refresh_token", WebAuthorization.TokenOption.REFRESHTOKEN);

                    appToken.refresh_token = item.Token.refresh_token;
                    appToken.Drive         = item.Token.Drive;
                }
                else if (item.Token.Drive == "OneDrive")
                {
                    appToken = await WebAuthorization.RedeemAuthorizationCodeAsync(Client.OneDriveClientID, UriCollection.OneDriveinstalledAppRedirectUri, Client.OneDriveClientSecret,
                                                                                   item.Token.refresh_token, UriCollection.OneDriveTokenUri, "refresh_token", WebAuthorization.TokenOption.REFRESHTOKEN);

                    appToken.refresh_token = item.Token.refresh_token;
                    appToken.Drive         = item.Token.Drive;
                }
                else if (item.Token.Drive == "DropBox")
                {
                    appToken = item.Token;
                }
                if (appToken != null)
                {
                    this.apptoken.Add(appToken);
                    Authentication.UserNameToken unametoken = new Authentication.UserNameToken();
                    DriveInfo driveinfo = null;
                    if (appToken.Drive == "Google")
                    {
                        var result = await HttpHelper.RequstHttp("GET", "https://www.googleapis.com/drive/v2/about", null, appToken.access_token);

                        Dictionary <string, object> drivetext = HttpHelper.DerealizeJson(result.GetResponseStream());
                        var map = new ApiDataMapping();
                        map.Add("name", "DisplayName");
                        map.Add("quotaBytesTotal", "TotalSize", typeof(long));
                        map.Add("quotaBytesUsed", "UseSize", typeof(long));
                        map.Add("quotaBytesUsedAggregate", "EmptySize", typeof(long));
                        map.Add("quotaBytesUsedInTrash", "DeleteSize", typeof(long));
                        map.Add("quotaType", "DriveType");
                        map.Add("rootFolderId", "DriveID");
                        Dictionary <string, object> di = (Dictionary <string, object>)drivetext["user"];
                        driveinfo        = new DriveInfo(drivetext, map);
                        driveinfo.UserID = di["emailAddress"].ToString();
                        driveinfo.Status = "normal";
                        driveinfo.token  = appToken;
                    }
                    else if (appToken.Drive == "OneDrive")
                    {
                        var result = await HttpHelper.RequstHttp("GET", "https://api.onedrive.com/v1.0/drive", null, appToken.access_token);

                        Dictionary <string, object> drivetext = HttpHelper.DerealizeJson(result.GetResponseStream());
                        Dictionary <string, object> owner     = (Dictionary <string, object>)drivetext["owner"];
                        Dictionary <string, object> user      = (Dictionary <string, object>)owner["user"];
                        Dictionary <string, object> quoat     = (Dictionary <string, object>)drivetext["quota"];
                        var map = new ApiDataMapping();
                        map.Add("id", "DriveID", typeof(string));
                        map.Add("driveType", "DriveType");
                        driveinfo             = new DriveInfo(drivetext, map);
                        driveinfo.DisplayName = user["displayName"].ToString();
                        driveinfo.UserID      = user["id"].ToString();
                        driveinfo.TotalSize   = long.Parse(quoat["total"].ToString());
                        driveinfo.EmptySize   = long.Parse(quoat["remaining"].ToString());
                        driveinfo.UseSize     = long.Parse(quoat["used"].ToString());
                        driveinfo.DeleteSize  = long.Parse(quoat["deleted"].ToString());
                        driveinfo.Status      = quoat["state"].ToString();
                        driveinfo.token       = appToken;
                    }
                    else if (appToken.Drive == "DropBox")
                    {
                        var result = await HttpHelper.RequstHttp("GET", "https://api.dropbox.com/1/account/info", null, appToken.access_token);

                        Dictionary <string, object> drivetext = HttpHelper.DerealizeJson(result.GetResponseStream());
                        Dictionary <string, object> quato     = (Dictionary <string, object>)drivetext["quota_info"];
                        var map = new ApiDataMapping();
                        map.Add("uid", "UserId", typeof(string));
                        map.Add("display_name", "DisplayName");
                        driveinfo           = new DriveInfo(drivetext, map);
                        driveinfo.TotalSize = long.Parse(quato["quota"].ToString());
                        driveinfo.UseSize   = long.Parse(quato["normal"].ToString());
                        driveinfo.EmptySize = driveinfo.TotalSize - driveinfo.UseSize;
                        driveinfo.token     = appToken;
                        driveinfo.Status    = "normal";
                    }
                    appToken.uid        = driveinfo.UserID;
                    unametoken.UserName = item.UserName;
                    unametoken.Token    = appToken;
                    drive.Add(driveinfo);
                    await Authentication.SaveToken(new FileDataStore(path), item.UserName, unametoken, CancellationToken.None);
                }
            }
            this.oncomplet(this);
            this.Stop();
        }
Example #2
0
        /// <summary>
        /// Oauth 2.0을 사용하여 유저 정보를 가져온다.
        /// </summary>
        /// <param name="clientId">Developer console에서 발급받은 userid</param>
        /// <param name="clientSecret">Developer console에서 발급받은 보안 번호</param>
        /// <param name="userName">사용자를 구별하기 위한 유저 이름 (닉네임)</param>
        /// <returns></returns>
        public async void CloudLogin()
        {
            string starturi  = null;
            string enduri    = null;
            string tokenuri  = null;
            string granttype = null;
            string drive     = null;

            if (option == LoginOption.GoogleDrive)
            {
                starturi  = UriCollection.GoogleAuthorizationUrl;
                enduri    = UriCollection.GoogleinstalledAppRedirectUri;
                tokenuri  = UriCollection.GoogleTokenUri;
                userName += "Google";
                granttype = "refresh_token";
                drive     = "Google";
            }
            else if (option == LoginOption.OneDrive)
            {
                starturi  = UriCollection.OneDriveAuthorizationUri;
                enduri    = UriCollection.OneDriveinstalledAppRedirectUri;
                tokenuri  = UriCollection.OneDriveTokenUri;
                userName += "OneDrive";
                granttype = "refresh_token";
                drive     = "OneDrive";
            }
            else
            {
                starturi  = UriCollection.DropBoxAuthorizationUri;
                tokenuri  = UriCollection.DropBoxTokenUri;
                userName += "DropBox";
                granttype = "authorization_code";
                drive     = "DropBox";
            }
            try
            {
                FileDataStore datastore       = new FileDataStore(path);
                UserNameToken oldRefreshToken = await LoadToken(datastore, userName, CancellationToken.None).ConfigureAwait(false);

                UserNameToken unametoken = new UserNameToken();
                TokenResult   appToken   = null;
                if (oldRefreshToken != null)
                {
                    if (oldRefreshToken.Token.refresh_token != null)
                    {
                        appToken = await WebAuthorization.RedeemAuthorizationCodeAsync(clientId, enduri, clientSecret, oldRefreshToken.Token.refresh_token, tokenuri, granttype, WebAuthorization.TokenOption.REFRESHTOKEN);

                        if (option == LoginOption.GoogleDrive)
                        {
                            appToken.refresh_token = oldRefreshToken.Token.refresh_token;
                        }
                        appToken.Drive = drive;
                    }
                    else
                    {
                        appToken = oldRefreshToken.Token;
                    }
                }
                // 신규 유저 접근 권한을 받아오거나 혹은 저장되어 있는 (기본 위치 C:\Users\bit-user\AppData\Roaming\)에 토큰을 가지고 유저정보를 가져온다.
                if (appToken == null)
                {
                    if (code != null)
                    {
                        appToken = await WebAuthorization.RedeemAuthorizationCodeAsync(clientId, enduri, clientSecret, code, tokenuri, "authorization_code", WebAuthorization.TokenOption.CODE);

                        appToken.Drive = drive;
                    }
                }
                if (appToken != null)
                {
                    if (appToken.Drive == "Google")
                    {
                        var result = await HttpHelper.RequstHttp("GET", "https://www.googleapis.com/drive/v2/about", null, appToken.access_token);

                        Dictionary <string, object> drivetext = HttpHelper.DerealizeJson(result.GetResponseStream());
                        var map = new ApiDataMapping();
                        map.Add("name", "DisplayName");
                        map.Add("quotaBytesTotal", "TotalSize", typeof(long));
                        map.Add("quotaBytesUsed", "UseSize", typeof(long));
                        map.Add("quotaBytesUsedAggregate", "EmptySize", typeof(long));
                        map.Add("quotaBytesUsedInTrash", "DeleteSize", typeof(long));
                        map.Add("quotaType", "DriveType");
                        map.Add("rootFolderId", "DriveID");
                        Dictionary <string, object> di = (Dictionary <string, object>)drivetext["user"];
                        driveinfo        = new DriveInfo(drivetext, map);
                        driveinfo.UserID = di["emailAddress"].ToString();
                        driveinfo.Status = "normal";
                        driveinfo.token  = appToken;
                    }
                    else if (appToken.Drive == "OneDrive")
                    {
                        var result = await HttpHelper.RequstHttp("GET", "https://api.onedrive.com/v1.0/drive", null, appToken.access_token);

                        Dictionary <string, object> drivetext = HttpHelper.DerealizeJson(result.GetResponseStream());
                        Dictionary <string, object> owner     = (Dictionary <string, object>)drivetext["owner"];
                        Dictionary <string, object> user      = (Dictionary <string, object>)owner["user"];
                        Dictionary <string, object> quoat     = (Dictionary <string, object>)drivetext["quota"];
                        var map = new ApiDataMapping();
                        map.Add("id", "DriveID", typeof(string));
                        map.Add("driveType", "DriveType");
                        driveinfo             = new DriveInfo(drivetext, map);
                        driveinfo.DisplayName = user["displayName"].ToString();
                        driveinfo.UserID      = user["id"].ToString();
                        driveinfo.TotalSize   = long.Parse(quoat["total"].ToString());
                        driveinfo.EmptySize   = long.Parse(quoat["remaining"].ToString());
                        driveinfo.UseSize     = long.Parse(quoat["used"].ToString());
                        driveinfo.DeleteSize  = long.Parse(quoat["deleted"].ToString());
                        driveinfo.Status      = quoat["state"].ToString();
                        driveinfo.token       = appToken;
                    }
                    else if (appToken.Drive == "DropBox")
                    {
                        var result = await HttpHelper.RequstHttp("GET", "https://api.dropbox.com/1/account/info", null, appToken.access_token);

                        Dictionary <string, object> drivetext = HttpHelper.DerealizeJson(result.GetResponseStream());
                        Dictionary <string, object> quato     = (Dictionary <string, object>)drivetext["quota_info"];
                        var map = new ApiDataMapping();
                        map.Add("uid", "UserId", typeof(string));
                        map.Add("display_name", "DisplayName");
                        driveinfo           = new DriveInfo(drivetext, map);
                        driveinfo.TotalSize = long.Parse(quato["quota"].ToString());
                        driveinfo.UseSize   = long.Parse(quato["normal"].ToString());
                        driveinfo.EmptySize = driveinfo.TotalSize - driveinfo.UseSize;
                        driveinfo.token     = appToken;
                        driveinfo.Status    = "normal";
                    }
                    appToken.uid        = driveinfo.UserID;
                    unametoken.UserName = this.userName;
                    unametoken.Token    = appToken;
                    SaveRefreshToken(unametoken, datastore, userName);
                    token = appToken;
                }
            }
            catch (Exception ex)
            {
            }
            this.OnComplete(this);
            this.Stop();
        }