使用App Access Token从Facebook帐户获取页面详细信息

时间:2014-05-19 08:52:21

标签: facebook api facebook-graph-api facebook-php-sdk facebook-sdk-4.0

我遇到了一个我没有找到任何解决方案或文档的问题。 我正在使用Facebook API构建一个应用程序,用户将创建一个应用程序,并将提供他们的APP ID& APP SECRET到应用程序以创建页面或发布到页面。

现在问题是他们只会登录我的应用程序,而不是登录facebook,也不能使用FB登录。

我从APP ID&amp ;;获得APP ACCESS TOKEN APP SECRET。
使用https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials

但我如何使用它来获取该帐户的页面或从中生成USER ACCESS TOKEN?

请帮助我提供专家指导。

感谢。

2 个答案:

答案 0 :(得分:0)

您无法使用图谱API创建应用程序或页面!见https://developers.facebook.com/docs/graph-api/reference/v2.0/page

如果您要向登录的用户管理员发布页面,您可以通过

请求相应的页面访问令牌(具有publish_actionsmanage_pages权限!)
GET /me/accounts

答案 1 :(得分:0)

首先创建开发人员应用并获取令牌提供一些权限

    public ActionResult Index()
    {

        var url = "http://www.facebook.com/v2.0/dialog/oauth/?scope=user_friends,read_friendlists,read_stream,read_insights,manage_pages,user_checkins,user_photos,read_mailbox,manage_notifications,read_page_mailboxes,email,user_videos,user_groups,offline_access,publish_actions,manage_pages&client_id=" + ConfigurationManager.AppSettings["ClientId"] + "&redirect_uri=" + ConfigurationManager.AppSettings["RedirectUrl"] + "&response_type=code";
        return Redirect(url);
    }

自动重定向回调网址,例如

    public ActionResult AddFacebookAccount(string code)
    {
        string ret = string.Empty;
        string client_id = ConfigurationManager.AppSettings["ClientId"];
        string redirect_uri = ConfigurationManager.AppSettings["RedirectUrl"];
        string client_secret = ConfigurationManager.AppSettings["ClientSecretKey"];
        long friendscount = 0;
        try
        {
            FacebookClient fb = new FacebookClient();
            string profileId = string.Empty;
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("client_id", client_id);
            parameters.Add("redirect_uri", redirect_uri);
            parameters.Add("client_secret", client_secret);
            parameters.Add("code", code);
            JsonObject fbaccess_token = null;
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                fbaccess_token = (JsonObject)fb.Get("/oauth/access_token", parameters);

            }
            catch (Exception ex)
            {

                try
                {
                    fbaccess_token = (JsonObject)fb.Get("/oauth/access_token", parameters);
                }
                catch (Exception ex1)
                {
                    ViewBag.acc_tkn= "issue_access_token";
                }
            }
            string accessToken = fbaccess_token["access_token"].ToString();
            Session["AccessToken"] = accessToken;
            if (accessToken != null)
            {
                fb.AccessToken = accessToken;
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
                dynamic profile = fb.Get("v2.2/me");
                dynamic friends = fb.Get("v2.2/me/friends");
                try
                {
                    Session["uid"] = profile.id;
                    friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }
                ViewBag.acc_tkn = accessToken;
                ViewBag.Uid = profile.id;
            }
            return View();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
            ViewBag.acc_tkn= "Something Went Wrong";
            return View();
        }
    }

当页面上的数据发布时参考Bellow Code

,您将获得访问令牌
    public void FacebookPostonPage(string file, string message, string tokenid)
    {
        JsonObject fbaccess_token = null;
        FacebookClient fb = new FacebookClient();
        fb.AccessToken = tokenid;
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
        fbaccess_token = (JsonObject)fb.Get("v2.0/me/accounts");
        dynamic Result = fbaccess_token["data"];
        foreach (var obj in Result)
        {
            string result = FacebookComposeMessage(obj.access_token, obj.id, message, file);
        }
    }     


    public string FacebookComposeMessage(string tokenid,string userid ,String message,string imagepath)
    {

        FacebookClient fb = new FacebookClient();
        string ret = "";
        fb.AccessToken = tokenid;
        fb.AppId = ConfigurationManager.AppSettings["ClientId"];
        fb.AppSecret = ConfigurationManager.AppSettings["ClientSecretKey"];
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
        var args = new Dictionary<string, object>();
        args["message"] = message;
        if (!string.IsNullOrEmpty(imagepath))
        {
            var media = new FacebookMediaObject
            {
                FileName = "filename",
                ContentType = "image/jpeg"
            };
            byte[] img = System.IO.File.ReadAllBytes(imagepath);
            media.SetValue(img);
            args["source"] = media;
            ret = fb.Post("v2.0/" + userid + "/photos", args).ToString();

        }
        else
        {
            ret = fb.Post("v2.0/" + userid + "/feed", args).ToString();
            //   ret = fb.Post("/" + objFacebookAccount.FbUserId + "/photos", args).ToString();
           // var data = fb.Get("v2.2" + ret);
        }
        return ret;
    }

您将成功发布,立即尝试

相关问题