Ejemplo n.º 1
0
        public RscGoogleAuth(RscJSonItem jsonClientSecret, string sGoogleScopes, WebBrowser wbAuthPage)
        {
            string sClientID     = jsonClientSecret.GetChildPropertyValue("installed", "client_id");
            string sClientSecret = jsonClientSecret.GetChildPropertyValue("installed", "client_secret");

            if (sClientID.Length == 0 || sClientSecret.Length == 0)
            {
                throw new Exception("Missing critical client_secret data!");
            }

            m_httpsAuth = new RscHttpsAuth("https://accounts.google.com")
            {
                ApprovalEndPoint = "/o/oauth2/approval",
                TokenEndPoint    = "/o/oauth2/token",
                AuthEndPoint     = "/o/oauth2/auth",
                RedirectUri      = "http://localhost",

                ClientId = sClientID,
                Secret   = sClientSecret,

                Scope = sGoogleScopes
            };

            m_httpsAuth.Authenticated        += new EventHandler(m_httpsAuth_Authenticated);
            m_httpsAuth.AuthenticationFailed += new EventHandler(m_httpsAuth_AuthenticationFailed);

            if (wbAuthPage != null)              //If null, non-UI mode, Tiles for example...
            {
                wbAuthPage.Navigating += new EventHandler <NavigatingEventArgs>(wbAuthPage_Navigating);
                wbAuthPage.Navigated  += new EventHandler <NavigationEventArgs>(wbAuthPage_Navigated);
                //wbAuthPage.NavigationFailed += new NavigationFailedEventHandler(wbAuthPage_NavigationFailed);
            }
        }
Ejemplo n.º 2
0
        private void DoLoad(TreeLbItem ti, string sContent, string sID, string sDetails, bool bSaveJSon)
        {
            string      sErr     = "";
            RscJSonItem jsonRoot = null;

            /*
             * jsonRoot = new RscJSonItem();
             * jsonRoot.ID = response.ResponseUri.ToString();
             * jsonRoot.Name = "IRestResponse<Object>";
             * jsonRoot.AddProperty( "Response Status", response.ResponseStatus.ToString() );
             * jsonRoot.AddProperty( "Response Uri", response.ResponseUri.ToString() );
             * jsonRoot.AddProperty( "Content Length", response.ContentLength.ToString() );
             */
            jsonRoot = RscJSon.FromResponseContetn(jsonRoot, sContent, out sErr, sID, sDetails);
            if (sErr.Length > 0)
            {
                LogError(sErr);
            }

            if (jsonRoot != null)
            {
                // //
                //

                string sErrorCode = "";
                if (jsonRoot.ChildCount > 0)
                {
                    if (jsonRoot.GetChild(0).Name == "error")
                    {
                        //For example: Required Scope not specified while LogOn!!!

                        sErrorCode = jsonRoot.GetChildPropertyValue(0, "code");
                        string sErrorMessage = jsonRoot.GetChildPropertyValue(0, "message");

                        LogError("Error response:\ncode: " + sErrorCode + "\nmessage: " + sErrorMessage);
                    }
                }

                //
                // //

                //Show Error JSon!!!
                //if( sErrorCode.Length == 0 )
                {
                    //Try to load result as is...
                    GoogleRequest gr = GoogleUtils.GoogleRequestFromUrl(jsonRoot.ID);

                    switch (gr)
                    {
                    case GoogleRequest.UserInfo:
                    case GoogleRequest.GMail_Messages:
                    case GoogleRequest.GMail_Message_Details:
                    case GoogleRequest.GMail_Labels:
                    case GoogleRequest.GMail_Threads:
                    case GoogleRequest.GMail_History:
                    case GoogleRequest.GMail_Drafts:
                    {
                        ti.SetResponse(jsonRoot);

                        RscStore store = new RscStore();

                        if (gr == GoogleRequest.UserInfo)
                        {
                            string sUserID = jsonRoot.GetPropertyValue("id");

                            if (m_sUserIDlast.Length == 0 || m_sUserIDlast != sUserID)
                            {
                                if (sUserID.Length > 0)
                                {
                                    m_sUserIDlast = sUserID;
                                    store.WriteTextFile(csSecretsFolder + "\\" + "UserIDlast.txt", m_sUserIDlast, true);

                                    AddRootContainers();
                                }
                            }
                        }

                        string sPath = "";
                        string sFn   = "";
                        if (m_sUserIDlast.Length > 0)
                        {
                            sPath = csSecretsFolder + "\\" + m_sUserIDlast;
                            sFn   = Uri2FileName(jsonRoot.ID);
                        }
                        if (bSaveJSon)
                        {
                            if (m_sUserIDlast.Length > 0)
                            {
                                store.CreateFolderPath(sPath);

                                store.WriteTextFile(sPath + "\\" + sFn + ".json", sContent, true);

                                m_btnCleanUp.Visibility = Rsc.Visible;
                            }
                        }

                        if (sErr.Length == 0)
                        {
                            if (sFn.Length > 0)
                            {
                                if (bSaveJSon)
                                {
                                    m_AppFrame.StatusText = "Downloaded to " + sPath + "\\" + sFn + ".json";
                                }
                                else
                                {
                                    m_AppFrame.StatusText = "Loaded from " + sPath + "\\" + sFn + ".json";
                                }
                            }
                            else
                            {
                                if (bSaveJSon)
                                {
                                    m_AppFrame.StatusText = "Downloaded...";
                                }
                                else
                                {
                                    m_AppFrame.StatusText = "Loaded...";
                                }
                            }
                        }

                        break;
                    }

                    default:
                    {
                        //Unexpected...
                        LogError(jsonRoot.ToDecoratedString());
                        break;
                    }
                    }
                }
            }
        }