I'm trying to figure out how I can get data from an apprequest response:
function invite(provider) {
FB.ui({
method: 'apprequests',
message: 'Check this thing out!',
title: 'Request Title'
}, function(response) {
if (response && !response.error) {
console.log('3 friend invited.');
console.log(response);
} else {
console.log('You have to invite 2 friends.');
console.log(response);
}
});
}
console.log(response); returns:
Object {
request: "667131760069164",
to: Array[1]
}
request: "667131760069164"
to: Array[1] 0: "1476807679266627"
length: 1
I'm wondering how I can get the information stored in to: to check how many were invited.
Thoughts, anyone?
Try this
obj = JSON.parse(response);
console.log(obj.to[0][0]);
console.log(obj.to[0].length);
You can access first value in 'to' array by obj.to[0][0] and obj.to[0].length
I managed to get the result I wanted with:
if (response.to.length > 2) {
console.log('Success! You have invited ' + response.to.length + ' friends.');
} else {
console.log('You need to invite at least 3 friends')
}
Thanks for the suggestions.
Related
Could anyone Show me an example of your finished Parameters and Endpoint for a Twitter Reply maybe with a Screenshot? Because i dont understand exactly what to type in my Params and, do I got to change anything in the Pre-request Script?
Kind regards Alex
For the Params for https://api.twitter.com/2/tweets I tried:
Key : in_reply_to
Value : tweet_id
And the Result was "errors"
"message": "The query Parameters [in_reply_to] is not one of [expantions,tweet.fields,media.fields,poll.fields,place.fields,user.fields]"
"title":"Invalid Request"
"detail": "One or more Parameters to your request Was invalid.",
"type":"https://api.twitter.com/2/problems/invalid-request"
From the twitter's documentation
query parameter ids is required. You missed that parameter.
I will get tweet this demo
https://twitter.com/pascal_bornet/status/1604754709000200193
BY Postman
Full code by node.js
#1 Get an access token by API Key and API secret
#2 Get text by access token
Credential in config.json
{
"API_KEY" : "7hK your API Key GND",
"API_KEY_SECRET" : "Zr4 your API Key secret 0qX0"
}
Save as get-tweet.js
const axios = require('axios')
const config = require('./config.json');
const getAccessToken = async () => {
try {
const resp = await axios.post(
'https://api.twitter.com/oauth2/token',
'',
{
params: {
'grant_type': 'client_credentials'
},
auth: {
username: config.API_KEY,
password: config.API_KEY_SECRET
}
}
);
// console.log(resp.data);
return Promise.resolve(resp.data.access_token);
} catch (err) {
// Handle Error Here
console.error(err);
return Promise.reject(err);
}
};
const getTweetText = async (token, tweet_id) => {
try {
const resp = await axios.get(
`https://api.twitter.com/2/tweets?ids=${tweet_id}`,
{
headers: {
'Authorization': 'Bearer '+ token,
}
}
);
return Promise.resolve(resp.data);
} catch (err) {
// Handle Error Here
console.error(err);
return Promise.reject(err);
}
};
getAccessToken()
.then((token) => {
console.log(token);
getTweetText(token, '1604754709000200193')
.then((result) => {
console.log(result.data[0].text);
})
})
Get Result
$ node get-tweet.js
AAAAAksadf--very long access token in here ----JlIMJIIse
Is this the future of Christmas shopping?
Credit: Nike
#innovation #AR # VR #AugmentedReality https://~~~
Am working on facebook graph API for posting on facebook page.I have a webpage where each users can login and share contents on a facebook page.Consider certain organizations,each organization has there own facebook page.From my website any user having facebook account can come and share their feedbacks about that organization and that feedback should be posted in the facebook page of the particular organization.
I need to implement this using facebook javascript api,but am getting an error
The user hasn't authorized the application to perform this action","type":"OAuthException","code":200
Here is my code:
FB.api('/page_id/feed', 'post',
{
message : "It's awesome ...",
name : 'Feedback',
to: '',
from: '',
description : 'Your description'
},
function(response) {
if (!response || response.error) {
//alert(JSON.stringify(response.error));
console.log(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
}
Please help
Thanks
Try this:
function login() {
FB.login(function (response) {
if (response.authResponse) {
// connected
postFeedBack();
} else {
// cancelled
}
}, { scope: 'publish_stream' });
}
function postFeedBack() {
FB.api('/page_id/feed', 'post', {
message: "My Feedback"
}, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
}
I am successfully authenticating a user to Facebook through my Spotify app, but then I try to request the user information and my .getJSON callback is never called. Here's my code :
auth.authenticateWithFacebook(facebookAppId, facebookPermissions, {
onSuccess: function(accessToken, ttl) {
var request_url = 'https://graph.facebook.com/me';
var url = request_url + '?access_token=' + accessToken;
$.getJSON(url, function(data) {
alert("Working");
});
},
onFailure: function(error) {
console.log("Authentication failed with error: " + error);
},
onComplete: function() { }
});
I tried with $.ajax, adding &callback=?, &callback=myFunction but nothing ever worked... Anyone have an idea of the issue?
Thanks
Make sure you add graph.facebook.com to the RequiredPermissions key in your manifest.json file.
Am trying to post to facebook wall from an android app written with phonegap. I do get the following error: message:(#200) the user hasn't authorized the application to perform this action type:OAuthException. So my question is How do i get the user to authorize my app so i can post to their wall. I use this code to login:
function login() {
FB.login(
function(response) {
if (response.session) {
alert('logged in');
} else {
alert('not logged in');
}
},
{ perms: 'publish_stream' }
);
}
and i try to post with the below code which throws an error.
function postToWall() {
var body = 'Reading Connect JS documentation';
FB.api('/me/feed', 'post', { body: body }, function(response) {
if (!response || response.error) {
alert('Error occured ' + JSON.stringify(response.error));
} else {
alert('Post ID: ' + response);
}
});
}
Am using the phonegap facebook api from https://github.com/davejohnson/phonegap-plugin-facebook-connect and FB.ui does not work.
Thanks
Posting the same content throws errors so use a text type input for positioning content on Facebook, with post as the text type input:
function postdata()
{
var body = document.getElementById("post").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error)
{
alert('Error occured');
}
else {
alert('Post ID: ' + response.id);
}
});
}
There are serious problems with that phonegap plugin right now. I'm really wishing it would be picked back up to fix them. One of the main issues is that oauth2 isn't supported and the plugin is using an older facebook sdk.
I dont know why but https://graph.facebook.com/132333806850364?access_token=218237124854683|VLsQV-ec2paQpq5hVMFFjtnk9_w returns false even though the app token is valid. The invitation was sent properly and I recieve invitations as well.
//Single user REquest
function sendRequestSingle(c_id,page_id,sendto,from,g_id) {
FB.ui({
method: 'apprequests',
to: sendto,
message: 'Win Mega Prizes!',
title: 'Enter in this contest!',
data: c_id+','+page_id+','+g_id
},
function (response) {
if (response && response.request_ids) {
var requests = response.request_ids;
window.location.href = "http://www.serverhere.com/contest/handle_invitations.php?invitations="+requests+"&contest_id="+c_id+"&page_id="+page_id+"&user="+from+"&g_id="+g_id+"&sendto="+sendto+"&single=1";
} else {
alert('canceled');
}
});
return false;
}