authenticate() public method

public authenticate ( string username, string password, string consumerKey, string consumerSecret ) : AuthenticationResult
username string
password string
consumerKey string
consumerSecret string
return AuthenticationResult
Esempio n. 1
0
        /// <summary>
        /// Logs into Evernote with the given username and password.
        /// </summary>
        private static AuthenticationResult Authenticate( string username, string password )
        {
            try
            {
                UserStore.Client userClient = new UserStore.Client( new TBinaryProtocol( new THttpClient( new Uri( BaseUrl + "/edam/user" ) ) ) );

                if ( !userClient.checkVersion( "EvernoteSharp", Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR, Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR ) )
                    throw new OldApiException( );

                return userClient.authenticate( username, password, ApiConsumerKey, ApiConsumerSecret );
            }
            catch ( EDAMUserException ex )
            {
                throw DecodeLoginFailure( ex );
            }
        }
Esempio n. 2
0
        public static void RunImpl(object state)
        {
            // Username and password of the Evernote user account to access
            const string username = ""; // Enter your username here
            const string password = ""; // Enter your password here

            if ((ConsumerKey == String.Empty) || (ConsumerSecret == String.Empty))
            {
                ShowMessage("Please provide your API key in Sample.cs");
                return;
            }
            if ((username == String.Empty) || (password == String.Empty))
            {
                ShowMessage("Please provide your username and password in Sample.cs");
                return;
            }

            // Instantiate the libraries to connect the service
            TTransport userStoreTransport = new THttpClient(new Uri(UserStoreUrl));
            TProtocol userStoreProtocol = new TBinaryProtocol(userStoreTransport);
            UserStore.Client userStore = new UserStore.Client(userStoreProtocol);

            // Check that the version is correct
            bool versionOK =
                userStore.checkVersion("C# EDAMTest",
                   Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR,
                   Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR);
            InvokeOnUIThread(() => ViewModel.TheViewModel.VersionOK = versionOK);
            Debug.WriteLine("Is my EDAM protocol version up to date? " + versionOK);
            if (!versionOK)
            {
                return;
            }

            // Now we are going to authenticate
            AuthenticationResult authResult;
            try
            {
                authResult = userStore.authenticate(username, password, ConsumerKey, ConsumerSecret);
            }
            catch (EDAMUserException ex)
            {
                HandleAuthenticateException(EvernoteHost, ConsumerKey, ex);
                return;
            }
            Debug.WriteLine("We are connected to the service");

            // User object received after authentication
            User user = authResult.User;
            String authToken = authResult.AuthenticationToken;
            InvokeOnUIThread(() => ViewModel.TheViewModel.AuthToken = authToken);

            Debug.WriteLine("Authentication successful for: " + user.Username);
            Debug.WriteLine("Authentication token = " + authToken);

            // Creating the URL of the NoteStore based on the user object received
            String noteStoreUrl = EDAMBaseUrl + "/edam/note/" + user.ShardId;

            TTransport noteStoreTransport = new THttpClient(new Uri(noteStoreUrl));
            TProtocol noteStoreProtocol = new TBinaryProtocol(noteStoreTransport);
            NoteStore.Client noteStore = new NoteStore.Client(noteStoreProtocol);

            // Listing all the user's notebook
            List<Notebook> notebooks = noteStore.listNotebooks(authToken);
            Debug.WriteLine("Found " + notebooks.Count + " notebooks:");
            InvokeOnUIThread(() => notebooks.ForEach(notebook => ViewModel.TheViewModel.Notebooks.Add(notebook)));

            // Find the default notebook
            Notebook defaultNotebook = notebooks.Single(notebook => notebook.DefaultNotebook);

            // Printing the names of the notebooks
            foreach (Notebook notebook in notebooks)
            {
                Debug.WriteLine("  * " + notebook.Name);
            }

            // Listing the first 10 notes in the default notebook
            NoteFilter filter = new NoteFilter { NotebookGuid = defaultNotebook.Guid };
            NoteList notes = noteStore.findNotes(authToken, filter, 0, 10);
            InvokeOnUIThread(() => notes.Notes.ForEach(note => ViewModel.TheViewModel.Notes.Add(note)));
            foreach (Note note in notes.Notes)
            {
                Debug.WriteLine("  * " + note.Title);
            }

            // Creating a new note in the default notebook
            Debug.WriteLine("Creating a note in the default notebook: " + defaultNotebook.Name);

            Note newNote = new Note
                            {
                                NotebookGuid = defaultNotebook.Guid,
                                Title = "Test note from EDAMTest.cs",
                                Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                          "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" +
                                          "<en-note>Here's an Evernote test note<br/>" +
                                          "</en-note>"
                            };

            Note createdNote = noteStore.createNote(authToken, newNote);

            ShowMessage("Successfully created new note with GUID: " + createdNote.Guid);
        }
Esempio n. 3
0
    public static void Main(string[] args) {

        if (args.Length < 2) {
            Console.WriteLine("Arguments:  <username> <password>");
            return;
        }
        String username = args[0];
        String password = args[1];

        // NOTE: You must change the consumer key and consumer secret to the 
        //       key and secret that you received from Evernote
        String consumerKey = "en-edamtest";
        String consumerSecret = "0123456789abcdef";
        
        String evernoteHost = "sandbox.evernote.com";
        String edamBaseUrl = "https://" + evernoteHost;
        // If using Mono, see http://www.mono-project.com/FAQ:_Security
        
        String userStoreUrl = edamBaseUrl + "/edam/user";
        TTransport userStoreTransport = new THttpClient(userStoreUrl);
        TProtocol userStoreProtocol = new TBinaryProtocol(userStoreTransport);
        UserStore.Client userStore = new UserStore.Client(userStoreProtocol);
        
        bool versionOK =
            userStore.checkVersion("C# EDAMTest",
        	   Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MAJOR,
        	   Evernote.EDAM.UserStore.Constants.EDAM_VERSION_MINOR);
        Console.WriteLine("Is my EDAM protocol version up to date? " + versionOK);
        if (!versionOK) {
            return;
        }
        
        AuthenticationResult authResult = null;
        try {
            authResult = userStore.authenticate(username, password,
                                                consumerKey, consumerSecret);
        } catch (EDAMUserException ex) {
            String parameter = ex.Parameter;
            EDAMErrorCode errorCode = ex.ErrorCode;
            
            Console.WriteLine("Authentication failed (parameter: " + parameter + " errorCode: " + errorCode + ")");
            
            if (errorCode == EDAMErrorCode.INVALID_AUTH) {
                if (parameter == "consumerKey") {
                    if (consumerKey == "en-edamtest") {
                        Console.WriteLine("You must replace the variables consumerKey and consumerSecret with the values you received from Evernote.");
                    } else {
                        Console.WriteLine("Your consumer key was not accepted by " + evernoteHost);
                        Console.WriteLine("This sample client application requires a client API key. If you requested a web service API key, you must authenticate using OAuth");
                    }
                    Console.WriteLine("If you do not have an API Key from Evernote, you can request one from http://www.evernote.com/about/developer/api");
                } else if (parameter == "username") {
                    Console.WriteLine("You must authenticate using a username and password from " + evernoteHost);
                    if (evernoteHost == "www.evernote.com" == false) {
                        Console.WriteLine("Note that your production Evernote account will not work on " + evernoteHost + ",");
                        Console.WriteLine("you must register for a separate test account at https://" + evernoteHost + "/Registration.action");
                    }
                } else if (parameter == "password") {
                    Console.WriteLine("The password that you entered is incorrect");
                }
            }
            
            return;
        }
        
        User user = authResult.User;
        String authToken = authResult.AuthenticationToken;
        Console.WriteLine("Authentication successful for: " + user.Username);
        Console.WriteLine("Authentication token = " + authToken);
        
        String noteStoreUrl = edamBaseUrl + "/edam/note/" + user.ShardId;
        TTransport noteStoreTransport = new THttpClient(noteStoreUrl);
        TProtocol noteStoreProtocol = new TBinaryProtocol(noteStoreTransport);
        NoteStore.Client noteStore = new NoteStore.Client(noteStoreProtocol);
        
        List<Notebook> notebooks = noteStore.listNotebooks(authToken);
        Console.WriteLine("Found " + notebooks.Count + " notebooks:");
        Notebook defaultNotebook = notebooks[0];
        foreach (Notebook notebook in notebooks) {
            Console.WriteLine("  * " + notebook.Name);
            if (notebook.DefaultNotebook) {
                defaultNotebook = notebook;
            }
        }
        
        Console.WriteLine();
        Console.WriteLine("Creating a note in the default notebook: " +
                          defaultNotebook.Name);
        Console.WriteLine();
        
        byte[] image = ReadFully(File.OpenRead("enlogo.png"));
        byte[] hash = new MD5CryptoServiceProvider().ComputeHash(image);
        string hashHex = BitConverter.ToString(hash).Replace("-", "").ToLower();
        
        Data data = new Data();
        data.Size = image.Length;
        data.BodyHash = hash;
        data.Body = image;
        
        Resource resource = new Resource();
        resource.Mime = "image/png";
        resource.Data = data;
        
        Note note = new Note();
        note.NotebookGuid = defaultNotebook.Guid;
        note.Title = "Test note from EDAMTest.cs";
        note.Content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" +
            "<en-note>Here's the Evernote logo:<br/>" +
            "<en-media type=\"image/png\" hash=\"" + hashHex + "\"/>" +
            "</en-note>";

        note.Resources = new List<Resource>();
        note.Resources.Add(resource);
        
        Note createdNote = noteStore.createNote(authToken, note);
        
        Console.WriteLine("Successfully created new note with GUID: " + createdNote.Guid);
    }