成功登录Facebook后需要获取个人资料信息

时间:2012-10-03 18:03:53

标签: asp.net c#-4.0 facebook-authentication

这个API的新用户我在一个使用Facebook API的网站上工作,允许他们登录。代码很旧,现在不能正常工作,所以我尝试升级使用新的SDK。我正确地让他们使用这个登录:

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function () {
    FB.init({
        appId: '<%= System.Configuration.ConfigurationManager.AppSettings["ApiID"] %>', // App ID
        channelUrl: 'http://<%= Request.ServerVariables["SERVER_NAME"] %>/channel.html', // Channel File
        status: true, // check login status
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true  // parse XFBML
    });

    // Additional initialization code here
};

// Load the SDK Asynchronously
(function (d) {
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) { return; }
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
} (document));
</script>

和这个链接:

<strong><a href="#" class="sIcoFB" onclick="FB.login(function(response) { if (response.authResponse) { scope: 'publish_stream,email'; parent.location = '<%=Server.UrlDecode(redirectPath)%>'; } else { alert('Please sign in to access the site'); } });">Login using Facebook</a></strong> &nbsp;&nbsp;  

这似乎有效,因为您可以打开facebook.com并且您已正确登录。但是,我似乎无法找到如何在C#中检查这一点。到目前为止我有这个:

//Facebook Authenticated, created session and API object
_connectSession = new FacebookClient();

//example of getting access code
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("client_id", System.Configuration.ConfigurationManager.AppSettings["ApiID"]);
parameters.Add("redirect_uri", Request.Url.AbsoluteUri);
parameters.Add("client_secret", System.Configuration.ConfigurationManager.AppSettings["Secret"]);
parameters.Add("scope", "publish_stream,email");

IDictionary<string, object> result = (IDictionary<string, object>)_connectSession.Get("/oauth/access_token", parameters);

string accessToken = (string)result["access_token"];

但是我收到了这个错误:

(OAuthException - #1)缺少授权码

如果有人能指出我在C#中的方向,我可以获取个人资料信息,因为我们已经在我们的数据库中拥有该ID,我只需要将其作为我们的一个用户登录。

2 个答案:

答案 0 :(得分:0)

您缺少获取access_token所需的code参数 - 该异常似乎是正确的。检查Authentication文档中的服务器端流程

您可能只需要将用户从auth对话框重定向回应用时收到的code参数包含在内 - 获取访问令牌的其他参数看起来正确

答案 1 :(得分:0)

我在下面有这个测试页面,这是来自Facebook开发人员的代码。它会提示您登录,然后将您的图片和名称放入元素中。虽然它正确登录,但它不会更新。

**更新了代码,现在可以使用 *

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testfb.aspx.cs" Inherits="testfb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<head id="Head1" runat="server">
    <title></title>    
    <script type="text/javascript" src="/js/jquery-1.6.1.min.js" charset="utf-8">    </script>        
    <script src="/js/jquery-ui-1.8.custom.min.js" type="text/javascript"></script>        
</head>
<body>                    
   <div id="fb-root"></div>
  <script>
      window.fbAsyncInit = function () {
          FB.init({
              appId: '111081862265698', // App ID
              channelUrl: '//<%= Request.ServerVariables["SERVER_NAME"] %>/channel.html', //     Channel File
              status: true, // check login status
              cookie: true, // enable cookies to allow the server to access the session
              xfbml: true  // parse XFBML
          });
          FB.login(function (response) {
              if (response.authResponse) {
                  alert('Welcome!  Fetching your information.... ');
                  FB.api('/me', function (response) {
                      alert('Good to see you, ' + response.name + '.');
                      var image = document.getElementById('image');
                      image.src = 'http://graph.facebook.com/' + response.id + '/picture';
                      var name = document.getElementById('name');
                      name.innerHTML = response.name
                  });
              } else {
                  alert('User cancelled login or did not fully authorize.');
              }
          });         
      };
      // Load the SDK Asynchronously
      (function (d) {
          var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
          if (d.getElementById(id)) { return; }
          js = d.createElement('script'); js.id = id; js.async = true;
          js.src = "//connect.facebook.net/en_US/all.js";
          ref.parentNode.insertBefore(js, ref);
      } (document));
  </script>

  <div align="center">
    <img id="image"/>
    <div id="name"></div>
  </div>
  <div class="fb-login-button" scope="email">
    Login with Facebook
  </div>

</body>
</html>
相关问题