2016-11-22 1 views
0

私はFacebook SDK for .NET 7.0.6を使用しています。SDK for .NETを使用してFacebookユーザーのIDを取得するにはどうすればよいですか?

このコードでは、ユーザーのIDではなく、名前、名字、姓などの属性を取得できます。

var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 
    dynamic resultMe = _fb.Get("me?fields=id"); 
    return resultMe.id; 

どうすれば入手できますか?

+0

どのように/私について?フィールドにid = ...? – luschn

+0

コードは例証に過ぎませんでした。あなたが言うように私はそれを得ることができません。 –

+0

はapiエクスプローラで正常に動作します:https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname&version=v2.8 – luschn

答えて

0

のIDが「App Scoped」の場合、ユーザーはAppごとに異なる(ただし一意の)IDを取得します。 APIエクスプローラでは、おそらく、あなたのアプリケーションを選択していないのですが、正式なAPIエクスプローラアプリケーションのみです。もちろん、別のIDになります。

つまり、IDを取得して、返品するユーザーを特定するのは正しいIDです。

+0

私はAPI Explorerを何度も使用してきましたが、私はいくつかのコード行を記述しましたが、JavaScript SDKを使用すると、私のエラーではないと確信しています。 –

+0

私の答えが正しいことを100%確信しています。あなたはIDを取得するのですか?あなたは何も得られないのではない。単にapiエクスプローラであなたのアプリを選択して、もう一度電話をかけると、同じIDを取得します。 – luschn

0

HomeController.cs

using Facebook; 
using Newtonsoft.Json.Linq; 
using System; 
using System.Collections.Generic; 
using System.Dynamic; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 

namespace FaceTest.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.UrlFb = GetFacebookLoginUrl(); 
      return View(); 
     } 

     public ActionResult About() 
     { 
      //ViewBag.Message = "Your application description page."; 
      ViewBag.Name = !String.IsNullOrEmpty(GetProfileName()) ? GetProfileName() : "Inicia Sesión"; 
      ViewBag.Id = !String.IsNullOrEmpty(GetProfileId()) ? GetProfileId() : "Inicia Sesión"; 
      return View(); 
     } 

     public ActionResult Contact() 
     { 
      ViewBag.Message = "Your contact page."; 

      return View(); 
     } 

     public string GetFacebookLoginUrl() 
     { 
      dynamic parameters = new ExpandoObject(); 
      parameters.client_id = "1766341193627031"; 
      parameters.redirect_uri = "http://localhost:51518/Home/RetornoFb/"; 
      parameters.response_type = "code"; 
      parameters.display = "page"; 

      var extendedPermissions = "publish_actions"; 
      parameters.scope = extendedPermissions; 

      var _fb = new FacebookClient(); 
      var url = _fb.GetLoginUrl(parameters); 

      return url.ToString(); 
     } 

     public ActionResult RetornoFb() 
     { 
      var _fb = new FacebookClient(); 
      FacebookOAuthResult oauthResult; 

      if(!_fb.TryParseOAuthCallbackUrl(Request.Url, out oauthResult)) 
      { 
       // Error 
      } 

      if (oauthResult.IsSuccess) 
      { 
       //Pega o Access Token "permanente" 
       dynamic parameters = new ExpandoObject(); 
       parameters.client_id = "1766341193627031"; 
       parameters.redirect_uri = "http://localhost:51518/Home/RetornoFb/"; 
       parameters.client_secret = "52cee8ef9437e4981302c24a66e13d55"; 
       parameters.code = oauthResult.Code; 

       dynamic result = _fb.Get("/oauth/access_token", parameters); 

       var accessToken = result.access_token; 

       //TODO: Guardar no banco 
       Session.Add("FbUserToken", accessToken); 
      } 
      else 
      { 
       // tratar 
      } 

      return RedirectToAction("Index"); 
     } 

     public ActionResult DetalhesDoUsuario() 
     { 
      if (Session["FbuserToken"] != null) 
      { 
       var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 

       //detalhes do usuario 
       var request = _fb.Get("me"); 
       var a = request; 
      } 

      return RedirectToAction("Index"); 
     } 
/* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 
     /* public string GetName(string externalToken) 
     { 
      FacebookClient client = new FacebookClient(externalToken); 
      dynamic resultMe = client.Get("me?fields=id,name"); 

      return resultMe.Name; 
     }*/ 

     public string GetProfileName() 
     { 
      if (Session["FbuserToken"] != null) 
      { 
       try 
       { 
        var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 
        dynamic resultMe = _fb.Get("me?fields=first_name"); 
        return resultMe.first_name; 
       } 
       catch (FacebookOAuthException) 
       { 
        return null; 
       } 
      } 

      return null; 
     } 

     public string GetProfileId() 
     { 

      if (Session["FbuserToken"] != null) 
      { 
       try 
       { 
        var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 
        dynamic resultMe = _fb.Get("me?fields=id"); 
        return resultMe.id; 
       } 
       catch (FacebookOAuthException) 
       { 
        return null; 
       } 
      } 

      return null; 
     } 
     /* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 
     /*public byte[] GetPhoto(string userId) 
      { 
       try 
       { 
        string url = "https://graph.facebook.com/" + userId + 
           "?fields=picture.width(720).height(720)"; 

        WebClient webClient = new WebClient(); 
        string response = webClient.DownloadString(url); 

        dynamic json = JObject.Parse(response); 

        string urlPicture = json.picture.data.url; 

        return webClient.DownloadData(urlPicture); 
       } 
       catch (Exception) 
       { 
        return null; 
       } 
      }*/ 
     public byte[] GetPhoto() 
     { 
      try 
      { 
       string url = "https://graph.facebook.com/" + GetProfileId() +"?fields=picture.width(480).height(480)"; 

       WebClient webClient = new WebClient(); 
       string response = webClient.DownloadString(url); 

       dynamic json = JObject.Parse(response); 

       string urlPicture = json.picture.data.url; 

       return webClient.DownloadData(urlPicture); 
      } 
      catch (Exception) 
      { 
       return null; 
      } 
     } 
/* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 
/* */ 
/* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 

    /* 
    protected IEnumerable<string> GetFriendIds(string externalToken) 
     { 
      FacebookClient client = new FacebookClient(externalToken); 
      dynamic result = client.Get("me/friends"); 

      foreach (dynamic friend in result.data) 
      { 
       yield return friend.id; 
      } 
     } 
    */ 
/* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 
     public ActionResult ListarAmigos() 
     { 
      if (Session["FbuserToken"] != null) 
      { 
       var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 

       //listar os amigos 
       var request = _fb.Get("me/friends"); 
       var a = request; 
      } 

      return RedirectToAction("Index"); 

     } 
/* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 
    /* 
    public void Share(string userId, string externalToken, Yipi yipi) 
     { 
      dynamic messagePost = new ExpandoObject(); 
      messagePost.link = GetYipiUrl(yipi); 
      messagePost.message = string.Format(TextMessage, yipi.Message); 

      FacebookClient client = new FacebookClient(externalToken); 
      client.Post(userId + "/feed", messagePost); 
     } 
    */ 
/* ------------------------------------------------------------------------------------------------------------------------------------------------ */ 
     public ActionResult PublicarMensagem() 
     { 
      if (Session["FbuserToken"] != null) 
      { 
       var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 

       //Postar uma mensagem na timeline 
       dynamic messagePost = new ExpandoObject(); 
       messagePost.picture = "http://www.rodolfofadino.com.br/wp-content/uploads/2013/12/image_thumb10.png"; 
       messagePost.link = "http://www.rodolfofadino.com.br/2013/12/test-mode-values-para-o-microsoft-advertising-sdk-windows-8/"; 
       messagePost.name = "Post name..."; 
       messagePost.caption = " Post Caption"; 
       messagePost.description = "post description"; 
       messagePost.message = "Mensagem de testes da aplicação"; 

       try 
       { 
        var postId = _fb.Post("me/feed", messagePost); 
       } 
       catch (FacebookOAuthException ex) 
       { 
        //handle oauth exception 
       } 
       catch (FacebookApiException ex) 
       { 
        //handle facebook exception 
       } 
      } 

      return RedirectToAction("Index"); 
     } 

     public ActionResult PublicarFoto() 
     { 
      if (Session["FbuserToken"] != null) 
      { 
       var _fb = new FacebookClient(Session["FbuserToken"].ToString()); 
       //upload de imagem 
       FacebookMediaObject media = new FacebookMediaObject 
       { 
        FileName = "Nome da foto", 
        ContentType = "image/jpeg" 
       }; 

       byte[] img = System.IO.File.ReadAllBytes(Server.MapPath("~/Content/rodolfo.jpg")); 
       media.SetValue(img); 

       dynamic parameters = new ExpandoObject(); 

       parameters.source = media; 
       parameters.message = "Descricao"; 

       try 
       { 
        dynamic result = _fb.Post("/me/photos", parameters); 

       } 
       catch (Exception ex) 
       { 
       } 
      } 
      return RedirectToAction("Index"); 
     } 
    } 
} 

index.cshtml

@{ 
    ViewBag.Title = "Home Page"; 
} 

<div class="jumbotron"> 
    <h1>ASP.NET + Facebook</h1> 
    <p><a href="@ViewBag.UrlFb" class="btn btn-primary btn-large">Login Facebook</a></p> 
</div> 

About.cshtml

@{ 
    ViewBag.Title = "About"; 
} 
<h2>@ViewBag.Title.</h2> 
<h3>@ViewBag.Name</h3> 

<p>@ViewBag.Id</p> 
+0

あなたが投稿した内容はわかりませんが、質問に対する回答ではありません。コードを追加する場合は、代わりに質問を編集してください。しかし、私はかなり追加コードが必要ではないと確信しています、私の答えを参照してください。コードを投稿する必要がある場合は、関連する部分だけを投稿してください。 – luschn

+0

ありがとう、すみません、私はこのプラットフォームが初めてです。 –

関連する問題