通过OAuth进行Evernote AuthToken

时间:2013-02-28 17:16:31

标签: c# api oauth evernote

之前我曾与OAuth合作(使用Twitter和PHP),这很简单。我试图让OAuth使用EverNote API示例https://github.com/evernote/evernote-sdk-csharp(因为,正如他们所说,“真实应用程序使用OAuth通过Evernote进行身份验证”)。我看了看这些:

Simple C# Evernote API OAuth example or guide?

https://github.com/sethhitch/csharp-oauth-sample

http://blog.stevienova.com/2008/04/19/oauth-getting-started-with-oauth-in-c-net/

但是,我仍然不知道该怎么做......这是我的代码:

    // Real applications authenticate with Evernote using OAuth, but for the
    // purpose of exploring the API, you can get a developer token that allows
    // you to access your own Evernote account. To get a developer token, visit 
    // https://sandbox.evernote.com/api/DeveloperToken.action
    String authToken = "myAuthCode";

    if (authToken == "your developer token") {
      Console.WriteLine("Please fill in your developer token");
      Console.WriteLine("To get a developer token, visit https://sandbox.evernote.com/api/DeveloperToken.action");
      return;
    }

如何向此添加OAuth以获取authToken

谢谢。

4 个答案:

答案 0 :(得分:7)

检查此示例项目:http://discussion.evernote.com/topic/30584-here-is-a-net-oauth-assembly/。我认为这将有助于您了解oauth的工作原理。

答案 1 :(得分:2)

对于任何想要让它在MVC中工作的人来说,今天早上我正在玩Evernote,OpenAuth和C#,并设法让它全部正常运行。我已经整理了一篇博客文章/图书馆,解释了这些经验并概述了如何在这里使用MVC - http://www.shaunmccarthy.com/evernote-oauth-csharp/ - 它使用了AsyncOAuth库:https://github.com/neuecc/AsyncOAuth

我在AsyncOAuth上写了一个你可能会觉得有用的包装器:https://github.com/shaunmccarthy/AsyncOAuth.Evernote.Simple

需要注意的一件事 - Evernote端点(/ oauth和/OAuth.action)区分大小写

// Download the library from https://github.com/shaunmccarthy/AsyncOAuth.Evernote.Simple

// Configure the Authorizer with the URL of the Evernote service,
// your key, and your secret. 
var EvernoteAuthorizer = new EvernoteAuthorizer(
    "https://sandbox.evernote.com", 
    "slyrp-1234", // Not my real id / secret :)
    "7acafe123456badb123");

// First of all, get a request token from Evernote - this causes a 
// webrequest from your server to Evernote.
// The callBackUrl is the URL you want the user to return to once
// they validate the app
var requestToken = EvernoteAuthorizer.GetRequestToken(callBackUrl);

// Persist this token, as we are going to redirect the user to 
// Evernote to Authorize this app
Session["RequestToken"] = requestToken;

// Generate the Evernote URL that we will redirect the user to in
// order to 
var callForwardUrl = EvernoteAuthorizer.BuildAuthorizeUrl(requestToken);

// Redirect the user (e.g. MVC)
return Redirect(callForwardUrl);

// ... Once the user authroizes the app, they get redirected to callBackUrl

// where we parse the request parameter oauth_validator and finally get
// our credentials
// null = they didn't authorize us
var credentials = EvernoteAuthorizer.ParseAccessToken(
    Request.QueryString["oauth_verifier"], 
    Session["RequestToken"] as RequestToken);

// Example of how to use the credential with Evernote SDK
var noteStoreUrl = EvernoteCredentials.NotebookUrl;
var noteStoreTransport = new THttpClient(new Uri(noteStoreUrl));
var noteStoreProtocol = new TBinaryProtocol(noteStoreTransport);
var noteStore = new NoteStore.Client(noteStoreProtocol);
List<Notebook> notebooks = client.listNotebooks(EvernoteCredentials.AuthToken);

答案 2 :(得分:1)

您还可以尝试在此处找到的OAuth库:https://code.google.com/p/devdefined-tools/wiki/OAuth并按照提及的步骤here进行操作。

答案 3 :(得分:0)

要添加的简单代码是:

EvernoteOAuth oauth = new EvernoteOAuth(EvernoteOAuth.HostService.Sandbox, myConsumerKey, myConsumerSecret);
        string errResponse = oauth.Authorize();
        if (errResponse.Length == 0)
        {
            Console.WriteLine(string.Format("Token: {0}\r\n\r\nExpires: {1}\r\n\r\nNoteStoreUrl: {2}\r\n\r\nUserId: {3}\r\n\r\nWebApiUrlPrefix: {4}", oauth.Token, oauth.Expires, oauth.NoteStoreUrl, oauth.UserId, oauth.WebApiUrlPrefix));
        }
        else
        {
            Console.WriteLine("A problem has occurred in attempting to authorize the use of your Evernote account: " + errResponse);
        }

您将需要使用此程序集:

using EvernoteOAuthNet;

可在此处获取:

http://www32.zippyshare.com/v/98249023/file.html