2014-03-03 18 views
0

我想建立一個網站,使用調查猴子API方法來顯示數據,爲此,我正在建立一個類庫,在其中我將調用一個(只有一個來自某個控制器)方法,該方法應該執行用戶的oauth (我在調查猴網站中提到的3步過程是成功的),但是現在我只想從控制器調用一個方法到類庫中的一個方法,該方法將執行用戶的oauth並設置可以稍後使用的令牌用於API方法。我的代碼是這樣的類庫:如何在類庫中使用重定向(url)方法?

HttpContext.Current.Response.Redirect(urlToAuthorize); 

//what should be here (I should wait till the user gives credentials and authorize so that I can get the query string) 

string tempAuthCode = HttpContext.Current.Session["code"].ToString(); 
if (!verifyRedirectedTempCode(tempAuthCode)) 
{ 
    return "Not a valid Token"; 
} 
else 
{ 
    try 
    { 
     var webRequest = GetWebRequestForHttpPostOfSurveyMonkeyToken(ApiKey,ClientSecret,tempAuthCode,RedirectUri,ClientId,GrantType,ContentType,WebRequestMethod); 
     using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse()) 
     { 
      string tokenJson = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
      AccessToken accesstokenObj = JsonConvert.DeserializeObject<AccessToken>(tokenJson); 
      return accesstokenObj.Token.ToString(); 
     } 
    } 
    catch 
    { 
     return null; 
    } 
} 

}

重定向後,它不是等待用戶authorize.So,它不工作完全按照我的想法。如何等待用戶授權並收集此查詢字符串?它應該在類庫本身完成。

回答

3

你不能像這樣處理它。 OAuth是一個兩階段過程。您重定向到OAuth提供商,然後一旦用戶在那裏授權,他們就會重定向到您的網站,在那裏您可以從中取走您的位置。

這意味着你需要兩個這樣的動作:一個啓動進程,一個授權後從提供程序接收有效載荷。

+0

ohhh ok謝謝... – user3324848