0

この API はまったく新しいので、Facebook API を使用してログインを許可する Web サイトに取り組んでいます。コードが古くて機能しないため、新しい 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) 認証コードがありません

データベースに ID が既にあるため、C# でプロファイル情報を取得する方法を誰かが教えてくれる場合は、ユーザーの 1 人としてログインするだけです。

4

2 に答える 2

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>
于 2012-10-04T19:00:44.843 に答える