2012-03-09 82 views
1
var facebookClient = new FacebookClient 
{ 
    ClientIdentifier = "appId", 
    ClientSecret = "appSecret" 
}; 
IAuthorizationState authorization = facebookClient.ProcessUserAuthorization(); 
if (authorization == null) 
{ 
    // Kick off authorization request 
    facebookClient.RequestUserAuthorization(); 
} 
else 
{ 

    var request = 
     WebRequest.Create("https://graph.facebook.com/me?access_token=" + 
         Uri.EscapeDataString(authorization.AccessToken)); 

    using (var response = request.GetResponse()) 
    { 
     using (var responseStream = response.GetResponseStream()) 
     { 
      var graph = FacebookGraph.Deserialize(responseStream); 
      lblFacebookUserName.Text = HttpUtility.HtmlEncode(graph.Name); 
     } 
    } 
} 

行至Facebook如何登錄使用DotNetOpenAuth

IAuthorizationState authorization = facebookClient.ProcessUserAuthorization(); 

總是返回null

+0

注意,在DotNetOpenAuth.ApplicationBlock的新版本,ClientSecret不存在。它現在應該如下所示:FacebookClient fbClient = new FacebookClient {ClientIdentifier =「appId」, ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(「appSecret」) }; – 2015-11-14 06:54:26

回答

1

我不是專家,但不是因爲你總是有它從來沒有一個新的FacebookClient請求用戶授權?您的工作流程覺得奇怪,我...

這是否有任何區別:

var facebookClient = new FacebookClient 
{ 
    ClientIdentifier = "appId", 
    ClientSecret = "appSecret" 
}; 
// Kick off authorization request, as have a new client that needs to be authorised 
facebookClient.RequestUserAuthorization(); 
// get the result of the authorization 
IAuthorizationState authorization = facebookClient.ProcessUserAuthorization(); 
if (authorization == null) 
{ 
    //throw not authorized exception or whatever 
} 
else 
{ 
    var request = 
     WebRequest.Create("https://graph.facebook.com/me?access_token=" + 
         Uri.EscapeDataString(authorization.AccessToken)); 

    using (var response = request.GetResponse()) 
    { 
     using (var responseStream = response.GetResponseStream()) 
     { 
      var graph = FacebookGraph.Deserialize(responseStream); 
      lblFacebookUserName.Text = HttpUtility.HtmlEncode(graph.Name); 
     } 
    } 
} 
+0

你有這個工作嗎?你可以發佈一些代碼嗎? – user1186651 2013-04-13 23:33:36

相關問題