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

Posted

技术标签:

【中文标题】使用 App Access Token 从 Facebook 帐户获取页面详细信息【英文标题】:Getting page details from Facebook account using App Access Token 【发布时间】:2014-05-19 08:52:20 【问题描述】:

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

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

我正在从 APP ID 和 APP SECRET 获取 APP ACCESS TOKEN。 使用https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials 但是我如何使用它来获取该帐户的页面或从中生成 USER ACCESS TOKEN?

请在您的专家指导下帮助我。

谢谢。

【问题讨论】:

【参考方案1】:

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

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

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

https://developers.facebook.com/docs/graph-api/reference/v2.0/user/accounts/ https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

【讨论】:

【参考方案2】:

首先创建开发者应用并获取令牌给予一些权限

    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);
    

例如自动重定向回调 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();
        
    

在页面上发布数据时,您将获得访问令牌参考波纹管代码

    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;
    

您将发布成功尝试现在

【讨论】:

以上是关于使用 App Access Token 从 Facebook 帐户获取页面详细信息的主要内容,如果未能解决你的问题,请参考以下文章

必须使用app access_token调用此方法

Facebook通知API:“必须使用app access_token调用此方法”

为啥我要传输 `client_secret` 来获取 `access_token`?

同账号小程序token和app token一样吗

如何在 access_token 中添加角色声明,目前它在 id_token 中?

试图获取应用访问令牌