2013-06-27 68 views
1

我想寫使用DotNetOpenAuth庫連接到使用OAuth的 Evernote的沙盒一個C#ASP.NET MVC應用程序,但我無法得到它的工作。我的應用沒有問題,直到回調被調用,但是當我嘗試請求在this diagram的步驟10中交換臨時憑證時,它會以401未授權的方式失敗。如何讓OAuth使用DotNetOpenAuth和Evernote?

我的回調看起來是這樣的:在var accessTokenResponse = webConsumer.ProcessUserAuthorization();線,是什麼企圖憑證交流發生

public ActionResult OAuthCallback() 
    { 
     var webConsumer = CreateWebConsumer(); 
     var accessTokenResponse = webConsumer.ProcessUserAuthorization(); 

     if (accessTokenResponse != null) 
     { 
      AccessToken = accessTokenResponse.AccessToken; 
     } 


     return RedirectToAction("Index"); 
    } 

的異常。

提琴手顯示以下內容:

調用回調:

GET http://localhost:22297/Home/OAuthCallback?oauth_token=GiddyUpHorsey.13F82BDC264.687474703A2F2F6C6F63616C686F73743A32323239372F486F6D652F4F4175746843616C6C6261636B.CFB67142944B4EB90148DDAFE2120A71&oauth_verifier=93534C2B04F862E57B30D738C3569242 HTTP/1.1 
Accept: text/html, application/xhtml+xml, */* 
Connection: Keep-Alive 
Accept-Language: en-NZ 
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) 
Pragma: no-cache 
Accept-Encoding: gzip, deflate 
Host: localhost:22297 
DNT: 1 
Cache-Control: no-cache 

請求令牌交換:

通過webConsumer.ProcessUserAuthorization();觸發。

POST https://sandbox.evernote.com/oauth HTTP/1.1 
Content-Type: application/x-www-form-urlencoded; charset=utf-8 
User-Agent: DotNetOpenAuth.Core/4.3.0.13117 
Host: sandbox.evernote.com 
Cache-Control: no-store,no-cache 
Pragma: no-cache 
Content-Length: 369 
Expect: 100-continue 

oauth_verifier=93534C2B04F862E57B30D738C3569242&oauth_token=GiddyUpHorsey.13F82BDC264.687474703A2F2F6C6F63616C686F73743A32323239372F486F6D652F4F4175746843616C6C6261636B.CFB67142944B4EB90148DDAFE2120A71&oauth_consumer_key=GiddyUpHorsey&oauth_nonce=cHABo5jv&oauth_signature_method=PLAINTEXT&oauth_signature=4c0dd81215379f75%26&oauth_version=1.0&oauth_timestamp=1372288061 

響應:

HTTP/1.1 401 Unauthorized 
Set-Cookie: JSESSIONID=4CDCD690AEAD69D952CEE4CBED5AC8DC; Path=/ 
Content-Type: text/html;charset=ISO-8859-1 
Content-Language: en 
Date: Wed, 26 Jun 2013 23:07:48 GMT 
Server: Evernote/1.0 
Content-Length: 1587 


<html> 
..... 
     <div class="page-header"> 
      <h1> 
      Oops, we encountered an error.</h1> 
     </div> 
     <div> 
      <p> 
      Sorry, we've encountered an unexpected error.</p> 
     </div> 
     <div class="clear"></div> 
     </div> 
... 
</html> 

(我去掉了大部分的HTML從響應)

爲什麼會失敗,401授權?

回答

2

我不確定你是否有這個工作,但我今天早上在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); 
+0

我想你的代碼和奇怪的是,我仍然得到401未經授權錯誤。我想知道這個問題是否與我的環境有關。該應用程序必須通過代理服務器與外部世界進行通信。也許這與它有關。 – GiddyUpHorsey

+0

你在哪一步得到401錯誤?用戶看到401還是在GetRequestToken期間發生? –

+0

它在調用https://sandbox.evernote.com/oauth這一行時發生:'var result = await base.GetAccessToken(OAuthUrl,token,oauth_verifier,null,null);'AsyncEvernoteoryAuthorizer.cs:Line 115。用戶已在Evernote中授權應用程序,然後發生錯誤。用戶看到有關401 Unauthorized異常的YSOD。 – GiddyUpHorsey

相關問題