首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >成功登录facebook后需要获取个人资料信息

问成功登录facebook后需要获取个人资料信息
EN

Stack Overflow用户
提问于 2012-10-04 02:03:53
回答 2查看 1.3K关注 0票数 0

这是一个全新的API,我在一个网站上工作,它使用Facebook API来允许他们登录。代码是旧的,现在不能工作,所以我试图升级到使用新的SDK。我正确地让他们使用以下命令登录:

代码语言:javascript
复制
<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>

和这个链接:

代码语言:javascript
复制
<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#中检查这一点。到目前为止,我有这样的想法:

代码语言:javascript
复制
//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,我只需要将他们作为我们的用户之一登录即可。

EN

回答 2

Stack Overflow用户

发布于 2012-10-04 02:29:51

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

您可能只需要包括用户从身份验证对话框重定向回您的应用程序时收到的code参数-用于获取访问令牌的调用中的其他参数看起来是正确的

票数 0
EN

Stack Overflow用户

发布于 2012-10-05 03:00:45

我有下面的测试页面,这是直接从Facebook开发人员的代码。它会提示您登录,然后将您的图片和姓名放入元素中。虽然它可以让您正确登录,但它不会更新。

**更新了代码,现在可以工作了*

代码语言:javascript
复制
<%@ 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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12714225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档