Calling new Dashboard API (dashboard.addNews) returns "news-action_link-href URL is not properly formatted" error

Feb 8, 2010 at 6:33 PM

I've been trying to call the new Facebook Dashboard API function, dasboard.addNews, with this code:

 

        var link = new Dictionary<string, string>();
	link.Add("text", "click me");
	link.Add("href", "http://nobody.com");

	var news = new Dictionary<string, string>();
	news.Add("message", "test");
	news.Add("action_link", JSONHelper.ConvertToJSONAssociativeArray(link));
            
	var rq = new Dictionary<string, string>();
	rq.Add("method", "Dashboard.addNews");
	rq.Add("uid", Api.Users.GetLoggedInUser().ToString());
	rq.Add("news", JSONHelper.ConvertToJSONAssociativeArray(news));
	var result = Api.SendRequest(rq);

and Facebook keeps returning the error "news-action_link-href URL is not properly formatted" -- even if I pass no action_link at all.

Any ideas what is wrong?

 

Feb 10, 2010 at 6:15 AM

here is my code that works but I hacked up the 2.0 toolkit and I used http://www.codeplex.com/Json

public string AddNews(List<DashboardSetNewsParams> news, string UserId)
        {
            JsonObjectCollection collection = new JsonObjectCollection();

            JsonObjectCollection collection2 = new JsonObjectCollection();
            var parameterList = new Dictionary<string, string> { { "method", "facebook.dashboard_addNews" } };
            var list = new List<string>();
            foreach (var item in news)
            {
                collection.Add(new JsonStringValue("message", item.Message));
                collection2.Add(new JsonStringValue("text", item.ActionLinkText));
                collection2.Add(new JsonStringValue("href", item.ActionLinkHref));
                collection.Add(new JsonObjectCollection("action_link", collection2));


            }
            parameterList.Add("uid", UserId);
            parameterList.Add("news", "["+collection.ToString()+"]");

            
            
            string response = _api.SendRequest(parameterList);
            return response;
       
        }

 

public class DashboardSetNewsParams
    {
        /// <summary>
        /// </summary>
        public string Message { get; set; }
        public string ActionLinkText { get; set; }
        public string ActionLinkHref { get; set; }
        
    }

Feb 10, 2010 at 6:24 AM

btw, where in the source did you find the dashboard.cs in the new toolkit?

I'm trying to figure out dashboard.publishActivity now. 

Feb 12, 2010 at 1:26 AM

I looked at http://www.codeplex.com/Json and I didn't see an objected called jsonObjectCollection.  Can you please tell me the full namespace of JsonObjectCollection?

 

Feb 12, 2010 at 2:50 AM

once you download the project you will find the dll called system.Net.Json.dll.

Feb 12, 2010 at 9:56 PM

gravitypushes, are you sure it's JsonObjectCollection?  I've looked at the project and can't find that object.  It's not even mentioned in the docs.  Maybe it was renamed recently?

 

Feb 12, 2010 at 10:00 PM

gravity, would you happen to have a sample of what the add news JSON looks like once you do the ToString?  At this point I want to just build the JSON manually.  I've tried that already but Facebook didn't seem to like something about my formatting.

 

Feb 12, 2010 at 10:14 PM

Well, here is a sample that works:

 

var rq = new Dictionary<string, string> { { "method", "facebook.dashboard_addNews" } };
rq.Add("uid", mp.Api.Users.GetLoggedInUser().ToString());
rq.Add("news", "[{\"message\":\"I just lost my nose\",\"action_link\":{\"href\":\"http://apps.facebook.com/pro-football-contes\",\"text\":\"Find out more!\"}}]");
var result = mp.Api.SendRequest(rq);

var rq = new Dictionary<string, string> { { "method", "facebook.dashboard_addNews" } };

rq.Add("uid", mp.Api.Users.GetLoggedInUser().ToString());

rq.Add("news", "[{\"message\":\"I just lost my nose\",\"action_link\":{\"href\":\"http://apps.facebook.com/test\",\"text\":\"Find out more!\"}}]");

var result = mp.Api.SendRequest(rq);

 

 

 

 

Feb 12, 2010 at 10:14 PM

The solution in this thread worked for me!

http://facebooktoolkit.codeplex.com/Thread/View.aspx?ThreadId=85065

 

Feb 26, 2010 at 10:33 AM

           Dictionary<string,string> methodParams = new Dictionary<string,string>();
            Dictionary<string, string> messageDic = new Dictionary<string, string>();
            Dictionary<string, string> actionLink = new Dictionary<string, string>();

            methodParams.Add("method","facebook.dashboard.addNews");
            methodParams.Add("uid","1000...........");  //User id deleted from sample
            methodParams.Add("image", "Image Url"); 

            actionLink.Add("text", "Link Text");
            actionLink.Add("href", "Link Url");

            List<string> actionLinkList = new List<string>();
            actionLinkList.Add(JSONHelper.ConvertToJSONAssociativeArray(actionLink));

            messageDic.Add("message", "My new message");
            messageDic.Add("action_link", JSONHelper.ConvertToJSONAssociativeArray(actionLink));
            List<string> news = new List<string>();

            news.Add(JSONHelper.ConvertToJSONAssociativeArray(messageDic));
                       
            Utilities.AddRequiredParameter(methodParams, "news", JSONHelper.ConvertToJSONArray(news));
            Console.WriteLine(Api.SendRequest(methodParams));

 

Mar 1, 2010 at 7:30 AM
Edited Mar 1, 2010 at 7:32 AM

 

Xsd2CodeSourceExtension.cs

.....................................

 

    [XmlRootAttribute("dashboard_publishActivity_response", Namespace = "http://api.facebook.com/1.0/", IsNullable = false)]

    public class dashboard_publishActivity_response : StringResponse { }

 

    [XmlRootAttribute("dashboard_addNews_response", Namespace = "http://api.facebook.com/1.0/", IsNullable = false)]

    public class dashboard_addNews_response : StringResponse { }

 

Dashboard.cs

using System;

using System.Collections.Generic;

     public string message { get; set; }
        public action_link action_link { get; set; }
    }
}

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Facebook.Session;

using Facebook.Schema;

using Facebook.Utility;

 

namespace Facebook.Rest

{

    /// <summary>

    /// Facebook Dashboard API methods

    /// </summary>

    public class Dashboard : RestBase

    {

        #region Methods

 

        #region Constructor

 

        /// <summary>

        /// Public constructor for facebook.Dashboard

        /// </summary>

        /// <param name="session">Needs a connected Facebook Session object for making requests</param>

        public Dashboard(FacebookSession session)

            : base(session)

        {

        }

 

        #endregion

 

        #region Public Methods

 

#if !SILVERLIGHT

 

        #region Synchronous Methods

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="uid"></param>

        /// <param name="news"></param>

        /// <returns></returns>

        public string AddNews(long uid, IList<dashboard_activity> news)

        {

            return AddNews(uid, null, news);

        }

 

        /// <summary>

        /// This method adds personal news from your application for the specified user. 

        /// </summary>

        /// <param name="uid">The ID of the user whose dashboard you are updating. If you include the uid, you must sign your call with your application secret. If you do not include the uid, you must include a session key and sign your call with a session secret.</param>

        /// <param name="image">The absolute URL to the image associated with the news item. Facebook formats the image as a 64x64 pixel square. This image cannot be a Facebook.com URL. If you don't specify an image, Facebook displays your application logo instead.</param>

        /// <param name="news">An array containing between 1 and 8 news item objects</param>

        /// <returns>This method returns an int containing a news_id if the call succeeds. Otherwise, it returns an error code. </returns>

        public string AddNews(long uid, string image, IList<dashboard_activity> news)

        {

            return AddNews(uid, image, news, false, null, null);

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="message"></param>

        /// <returns></returns>

        public string PublishActivity(string message)

        {

            return PublishActivity(message, null);

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="message"></param>

        /// <param name="action_link"></param>

        /// <returns></returns>

        public string PublishActivity(string message, action_link action_link)

        {

            return PublishActivity(message, action_link, false, null, null);

        }

 

        #endregion

 

#endif

        #region Asynchronous Methods

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="uid"></param>

        /// <param name="image"></param>

        /// <param name="news"></param>

        /// <param name="callback"></param>

        /// <param name="state"></param>

        /// <returns></returns>

        public string AddNewsAsync(long uid, string image, IList<dashboard_activity> news, AddNewsCallback callback, Object state)

        {

            return AddNews(uid, image, news, true, callback, state);

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="messge"></param>

        /// <param name="callback"></param>

        /// <param name="state"></param>

        /// <returns></returns>

        public string PublishActivityAsync(string messge, PublishActivityCallback callback, Object state)

        {

            return PublishActivityAsync(messge, null, callback, state);

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <param name="message"></param>

        /// <param name="action_link"></param>

        /// <param name="callback"></param>

        /// <param name="state"></param>

        /// <returns></returns>

        public string PublishActivityAsync(string message, action_link action_link, PublishActivityCallback callback, Object state)

        {

            return PublishActivity(message, action_link, true, callback, state);

        }

 

        #endregion

 

        #endregion

 

        #region Private Methods

 

        private string AddNews(long uid, string image, IList<dashboard_activity> news, bool isAsync, AddNewsCallback callback, Object state)

        {

            var parameterList = new Dictionary<string, string> { { "method", "facebook.dashboard.addNews" } };

            Utilities.AddRequiredParameter(parameterList, "uid", uid);

 

            var list = new List<string>();

            foreach (var item in news)

            {             

                var activity = new Dictionary<string, string>()

                {

                    {"message", item.message}

                };

 

                if (item.action_link != null)

                {

                    var dict = new Dictionary<string, string>

                    {

                        {"text", item.action_link.text},

                        {"href", item.action_link.href}

                    };

                    activity.Add("action_link", JSONHelper.ConvertToJSONAssociativeArray(dict));

                }

                list.Add(JSONHelper.ConvertToJSONAssociativeArray(activity));

 

            }

            Utilities.AddJSONArray(parameterList, "news", list);

            Utilities.AddOptionalParameter(parameterList, "image", image);

 

            if (isAsync)

            {

                SendRequestAsync<dashboard_addNews_response, string>(parameterList, uid <=0, new FacebookCallCompleted<string>(callback), state);

                return null;

            }

            var response = SendRequest<dashboard_addNews_response>(parameterList, uid <= 0);

            return response == null ? null : response.TypedValue;

        }

 

        private string PublishActivity(string message, action_link action_link, bool isAsync, PublishActivityCallback callback, Object state)

        {

            var parameterList = new Dictionary<string, string> { { "method", "facebook.dashboard.publishActivity" } };

 

            var activity = new Dictionary<string, string>

            {

                {"message", message},

            };

            if (action_link != null)

            {

                var dict = new Dictionary<string, string>

                {

                    {"text", action_link.text},

                    {"href", action_link.href}

                };

                activity.Add("action_link", JSONHelper.ConvertToJSONAssociativeArray(dict));

            }

            Utilities.AddRequiredParameter(parameterList, "activity", JSONHelper.ConvertToJSONAssociativeArray(activity));

 

            if (isAsync)

            {

                SendRequestAsync<dashboard_publishActivity_response, string>(parameterList, new FacebookCallCompleted<string>(callback), state);

                return null;

            }

            var response = SendRequest<dashboard_publishActivity_response>(parameterList);

            return response == null ? null : response.TypedValue;

        }

 

        #endregion

 

        #endregion

 

        #region Delegates

 

 

        /// <summary>

        /// Delegate called when AddNews call completed

        /// </summary>

        /// <param name="news_id">New identifer</param>

        /// <param name="state">An object containing state information for this asynchronous request</param>

        /// <param name="e">Exception object, if the call resulted in exception.</param>

        public delegate void AddNewsCallback(string news_id, Object state, FacebookException e);

 

        /// <summary>

        /// Delegate called when PublishActivity call completed

        /// </summary>

        /// <param name="activiy_id">Activity identifer</param>

        /// <param name="state">An object containing state information for this asynchronous request</param>

        /// <param name="e">Exception object, if the call resulted in exception.</param>

        public delegate void PublishActivityCallback(string activiy_id, Object state, FacebookException e);

 

        #endregion

    }

 

    /// <summary>

    /// helper class for calls to dashboard.addNews with news

    /// </summary>

    public class dashboard_activity

    {

        public string message { get; set; }

 

        public action_link action_link { get; set; }

    }

}

 

Api.cs

public class Api : RestBase

{

.........................

 

        /// <summary>

        /// Gets or sets the Dashboard API object instance

        /// </summary>

        public Dashboard Dashboard { get; private set; }

 

 

public Api(FacebookSession session) : base(session) {

.......

Dashboard = new Dashboard(session);

 

foreach (RestBase restBase in new RestBase[] {Auth, Video, Marketplace, Admin, Photos, Users, Friends, Events,

Groups, Notifications, Profile, Fbml, Feed, Fql, LiveMessage, Message, Pages, Application, Data, Connect, Comments,

Stream, Status, Links, Notes, Dashboard})

{

restBase.Batch = Batch;

restBase.Permissions = Permissions;

}

     }

 

}

Example:

            string image = "http://www.martialdevelopment.com/wordpress/wp-content/images/cheezburger-or-dim-mak.jpg";

            dashboard_activity activity = new dashboard_activity()

            {

                message = "Your friend @:563683308 just sent you a present",

                action_link = new action_link()

                {

                    text = "Get Your Gift",

                    href = "http://www.example.com/gifts?id=5878237'"

                }

            };

 

            api.Dashboard.AddNews(uid, image, new List<dashboard_activity>() { activity });


 

 

Mar 2, 2010 at 5:16 PM

Thank you so much for this, really nice code!!

I have a question though: I get the message id back correctly after calling with your example but I cannot see the message anywhere when I log in with the user I sent it to. Is simply calling "addNews" enough or do you need to call something else es well e.g. setNews?

 

Thank you very much

Miklos

Mar 3, 2010 at 2:53 AM

hi,

Only call addNews

The Facebook Dashboard API lets you integrate your application into the Application Dashboard or the Games Dashboard.

http://wiki.developers.facebook.com/index.php/Dashboard_API

Mar 3, 2010 at 10:00 AM

Hi,

Yes, I do that that but the message is not shown in the Applications dashboard. I do get back the message id though.

Do I need to change any settings in the application at http://www.facebook.com/developers/?

I had this application running for a long time and it was using Notifications to send information to users. But as you know of 1st March FB discontinued it so I was simply switching to this new method - and I don't get any errors but I don't see any results either.

Any ideas??

Thank you,

Miklos

Mar 3, 2010 at 10:42 AM

Hi,

I have tested the code working. Don't change any settings.

This new method sends message to users, so only users can see message in their applications dashboard. 

let try to use this method to send message to you.

Sorry my poor english. 

 

Mar 3, 2010 at 12:49 PM

Hi,

No worries, and thanks! That was a good tip - so I do get the messages myself! And they look really cool.

But nobody else gets the messages - I sent it to a test account + other people around me and they don't get it.

Has this got to do with some security settings maybe?

Miklos

Mar 3, 2010 at 1:53 PM

Hi,

I know this a .NET forum, but I have a question for you guys I'm trying to send the following "news" message to Dashboard.addNews using some Java code


[{"message":"testing news","action_link":{"text":"my text","href":"http://example.com"}}]

The response looks correct, there's a news_id:

<?xml version="1.0" encoding="UTF-8"?>
<Dashboard_addNews_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">339827219620</Dashboard_addNews_response>

but nothing is displayed in the targeted Facebook user profile, any idea?

Regards,

E

Mar 3, 2010 at 2:35 PM

This is exacly my problem - and also a few other people's:

http://forum.developers.facebook.com/viewtopic.php?pid=207798

THe response is correct but then nothing is displayed - apart from the case if you're posting it to your own profile. Which is not very useful

 

Mar 3, 2010 at 3:48 PM
Edited Mar 3, 2010 at 3:50 PM

Hi guy,

Dashboard API is still beta.

Mar 4, 2010 at 9:50 AM
Edited Mar 4, 2010 at 10:21 AM

Hi guy

This addNews method adds personal news from application for the specified user.

This method required session_key. The session key of the logged in user, or the session key provided when the user granted your application the offline_access extended permission

So the user who not granted application the offline_access can not get personal news.

Mar 18, 2010 at 3:46 AM
marcinz wrote:

           Dictionary<string,string> methodParams = new Dictionary<string,string>();
            Dictionary<string, string> messageDic = new Dictionary<string, string>();
            Dictionary<string, string> actionLink = new Dictionary<string, string>();

            methodParams.Add("method","facebook.dashboard.addNews");
            methodParams.Add("uid","1000...........");  //User id deleted from sample
            methodParams.Add("image", "Image Url"); 

            actionLink.Add("text", "Link Text");
            actionLink.Add("href", "Link Url");

            List<string> actionLinkList = new List<string>();
            actionLinkList.Add(JSONHelper.ConvertToJSONAssociativeArray(actionLink));

            messageDic.Add("message", "My new message");
            messageDic.Add("action_link", JSONHelper.ConvertToJSONAssociativeArray(actionLink));
            List<string> news = new List<string>();

            news.Add(JSONHelper.ConvertToJSONAssociativeArray(messageDic));
                       
            Utilities.AddRequiredParameter(methodParams, "news", JSONHelper.ConvertToJSONArray(news));
            Console.WriteLine(Api.SendRequest(methodParams));

 

 I am using your code example and it looks like the request works and I get back a valid response:

<?xml version="1.0" encoding="UTF-8"?>

<dashboard_addNews_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">375882459274</dashboard_addNews_response>

but when I log into my facebook account I don't see the news entry.  Do I need to set the SessionSecret or some other property for this to work properly?

Mar 18, 2010 at 11:49 AM

There are several tricks.

-As far as I know your application must be type of desktop (even it de facto this is web page)

-You have to use correct session information. I first checked this code with normal login to facebook, than with pernament session.

-News is on the correct dashboard.  (Application / Game)

-User must autorize your application.

-I have set in config file all of these:

  <add key="ApiKey" value="my key"/>
  <add key="Secret" value="my secret"/>
  <add key="Callback" value="http://www.mydomainat.hostedwindows.pl/"/>
  <add key="Suffix" value="myapplicationsufix"/>
  <add key="CSSVersion" value="1"/>
  <add key="JSVersion" value="1"/>

(first 4 parameters changed due to privacy :) )

 

 

Mar 24, 2010 at 5:53 PM
mkanyo wrote:

Hi,

No worries, and thanks! That was a good tip - so I do get the messages myself! And they look really cool.

But nobody else gets the messages - I sent it to a test account + other people around me and they don't get it.

Has this got to do with some security settings maybe?

Miklos

 Have you been able to resove this issue?  I am experiencing the same thing, I have one application that can send news but I am the only one that can see it.  I've tried sending news to other people and they do not see it.  The other strange thing is I have other applications which are identical to the app that works and I don't see any news items when sent from those, not even on my page.  Any information would be greatly appreciated.

Mar 25, 2010 at 3:37 PM

I belive you should have pernament session information for each user you send news.

Every potential target should grant correct permission to appliation - try to check on several facebook dev accounts...

MZ