utilizing the JavaScript SDK to send alerts to a user's friends on Facebook

I am attempting to send a notification to a user's friend using the JS SDK on a Facebook canvas app, but I'm encountering this error in the console:

POST https://graph.facebook.com/16542203957691/notifications? 400 (OK) jquery.min.js:140
c.extend.ajax jquery.min.js:140
c.extend.post jquery.min.js:133
makePost ec2-34-41-111-91.ap-southwest-8.compute.amazonaws.com/:69
onclick

I have created a function called makePost that takes the friend's profile ID as an argument. Here is my implementation:

function makePost(personid){
  var accesstoken =   FB.getAuthResponse()['accessToken'];
  var address = "https://graph.facebook.com/" + personid + "/notifications?";
  var tempdata = {};
  tempdata['access_token'] = accesstoken;
  tempdata['href'] = "";
  tempdata['template'] = "You have earned 5 credits, Click here to redeem";
  jQuery.post(address, {json: JSON.stringify(tempdata)}, function(data){
      console.log(data);
  });
}

Despite implementing this, the intended recipient is not receiving the notification.

Answer №1

The issue was that the access token needed to be a combination of the app id and app secret, separated by the "|" symbol. Additionally, the data should be sent as an array instead of a JSON object.

Here is the updated code snippet with the correct format:

function makePost(personid){
  var address = "https://graph.facebook.com/" + personid + "/notifications";
  var tempdata = {};
  tempdata['access_token'] = appId + "|" + appSecret;
  tempdata['href'] = "";
  tempdata['template'] = "You have earned 5 credits, Click here to redeem";
  jQuery.post(address, tempdata , function(data){
      console.log(data);
  });
}

Note: This code uses the JQuery library for making the post request, so remember to include jquery.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps are involved in creating a video playlist with YouTube videos?

Is there a way to create a dynamic video playlist that supports embedded YouTube videos without refreshing the page? If a user clicks on another video, I want the video to change dynamically. You can check out this for an example. Do jPlayer, Video.js, Fl ...

authorization for certain express routes using passport.js

Securing certain express routes with Passport.js authentication Steps for authenticating specific routes in Passport.js Looking for a method to authenticate particular routes using Passport.js Your assistance is greatly appreciated... ...

Updating part of a page while also changing the navigation

Apologies in advance, as this is my first attempt at coding a website. I have a specific need where I want to update only one div on my web page when a link in the navigation bar is clicked. <body> <div id="wrapper"> <header id= ...

Exploring JSON Data with the Wikipedia API

My current project involves extracting text from Wikipedia articles using their API, which is not the most user-friendly tool. I am facing challenges with parsing the JSON object that I receive in return. The key containing the desired text is labeled &apo ...

Issue: Attempting to render objects as React children is invalid. Consider using an array if you want to render a collection of children. This problem often occurs when fetching data from a JSON

Hey everyone, I'm currently working on a small website using ReactJS. After adding the code below, an error keeps popping up: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead. Here's t ...

JavaScript Firebase: Service worker malfunctioning during navigation

I'm currently developing a website that relies on firebase messaging. To make this happen, a specialized service worker for firebase has been integrated into the site. This website functions as a webchat platform where messages are synchronized and s ...

What is the best way to target the nth-child() of a slotted element within a web component that utilizes multiple uniquely named slots?

I am struggling to select the second slotted item in a specific slot using slot[name=foo]::slotted(:nth-child(2)){, but it's not behaving as I anticipated. Even though the first foo slot is styled with green, the second one doesn't follow suit. ...

Struggling to get the knockout js remove function to function properly within a nested table structure

I've been encountering issues while trying to eliminate the formulation elements with the 'Delete comp' link. I can't seem to figure out why it's not functioning as expected. Moreover, the 'Add another composition' link o ...

What methods can be used to defer the visibility of a block in HTML?

Is there a way to reveal a block of text (h4) after a delay once the page has finished loading? Would it be necessary to utilize setTimeout for this purpose? ...

Is there a way to dynamically integrate the Insert Column feature into the ContextMenu of a Syncfusion Treegrid?

What is the best way to add Insert Column functionality to the ContextMenu of a Syncfusion Treegrid? Insert Column Rename Column Delete Column ...

Is it possible to display or hide a spinner within a span using Javascript and AJAX?

When submitting a contact form, I want to display and hide a spinner on the submit button. The spinner should be visible while the form is being submitted and hidden once the submission is complete. Below is the code for my submit button with the spinner: ...

What is the method for sending raw put data using the request npm package in a Node.js environment

How can I hit an API using the "require" npm package in Node? The API requires raw PUT data instead of PUT fields. Can anyone please guide me on how to achieve this using the request npm package? For example, here is the raw PUT data that needs to be sent ...

Seeking assistance in identifying the highest value in JavaScript (excluding the use of the max method)

Greetings and thank you for stopping by. I'm new to Javascript and currently facing an issue that I could use some help with. I am trying to identify the highest number from a set of variables using conditional statements within a function, but I seem ...

Is there a way to repurpose a JavaScript object that gets sent back to the client upon page loading?

Upon initial page load, an AJAX request fetches a list of JavaScript objects which display only one value each. The page includes buttons to expand each item and reveal the remaining values. Originally, I used another AJAX call to retrieve individual objec ...

What is the best way to implement a new search input for my datatable while also enabling the searchHighlight feature?

I'm attempting to implement a search bar for my datatables. I need to hide the default search engine that comes with datatables, so I added a script that I found in a forum. However, when I try to run it in my code, it doesn't work and displays a ...

Top method for preventing an HTTP 403 error when receiving the Set-Cookie header in order to establish the CSRF Cookie

As I interact with a REST API that includes CSRF protection measures, I am facing a common hurdle. Successfully obtaining the token and sending it back to the server seems to work smoothly. However, encountering an HTTP 403 error arises when initiating t ...

Effective steps after Ajax request

Whenever a user clicks the "like" button, I perform a check in the database to see if they have already liked the photo. This check is done using ajax. If the photo has already been liked, the success message will indicate "already liked", otherwise it wi ...

I keep encountering the error message "ReferenceError: window is not defined" in Next.js whenever I refresh the page with Agora imported. Can someone explain why this is happening?

Whenever I refresh my Next.js page with Agora SDK imported, I keep encountering the error "ReferenceError: window is not defined". It seems like the issue is related to the Agora import. I attempted to use next/dynamic for non-SSR imports but ended up with ...

The issue with session storage persisting even after closing the iframe

Encountering a persistent issue where the sessionStorage remains populated even after closing an iframe and opening another one with the same destination. I assumed that the sessionStorage would be reset and start afresh each time. The iframe is contained ...

Vows: proceed to the subsequent error handling process

Can you explain how to properly call the next error function using promise chaining? I initially believed that placing a return statement within the error function would automatically trigger the next error function. //This code is executed in a contr ...