I am trying to get my friend name and ids with Graph API v2.0, but data returns empty:
{
"data": [
]
}
When I was using v1.0, everything was OK with the following request:
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSArray* friends = [result objectForKey:#"data"];
NSLog(#"Found: %i friends", friends.count);
for (NSDictionary<FBGraphUser>* friend in friends) {
NSLog(#"I have a friend named %# with id %#", friend.name, friend.id);
}
}];
But now I cannot get friends!
In v2.0 of the Graph API, calling /me/friends returns the person's friends who also use the app.
In addition, in v2.0, you must request the user_friends permission from each user. user_friends is no longer included by default in every login. Each user must grant the user_friends permission in order to appear in the response to /me/friends. See the Facebook upgrade guide for more detailed information, or review the summary below.
If you want to access a list of non-app-using friends, there are two options:
If you want to let your people tag their friends in stories that they publish to Facebook using your App, you can use the /me/taggable_friends API. Use of this endpoint requires review by Facebook and should only be used for the case where you're rendering a list of friends in order to let the user tag them in a post.
If your App is a Game AND your Game supports Facebook Canvas, you can use the /me/invitable_friends endpoint in order to render a custom invite dialog, then pass the tokens returned by this API to the standard Requests Dialog.
In other cases, apps are no longer able to retrieve the full list of a user's friends (only those friends who have specifically authorized your app using the user_friends permission). This has been confirmed by Facebook as 'by design'.
For apps wanting allow people to invite friends to use an app, you can still use the Send Dialog on Web or the new Message Dialog on iOS and Android.
UPDATE: Facebook have published an FAQ on these changes here: https://developers.facebook.com/docs/apps/faq which explain all the options available to developers in order to invite friends etc.
Although Simon Cross's answer is accepted and correct, I thought I would beef it up a bit with an example (Android) of what needs to be done. I'll keep it as general as I can and focus on just the question. Personally I wound up storing things in a database so the loading was smooth, but that requires a CursorAdapter and ContentProvider which is a bit out of scope here.
I came here myself and then thought, now what?!
The Issue
Just like user3594351, I was noticing the friend data was blank. I found this out by using the FriendPickerFragment. What worked three months ago, no longer works. Even Facebook's examples broke. So my issue was 'How Do I create FriendPickerFragment by hand?
What Did Not Work
Option #1 from Simon Cross was not strong enough to invite friends to the app. Simon Cross also recommended the Requests Dialog, but that would only allow five requests at a time. The requests dialog also showed the same friends during any given Facebook logged in session. Not useful.
What Worked (Summary)
Option #2 with some hard work. You must make sure you fulfill Facebook's new rules: 1.) You're a game 2.) You have a Canvas app (Web Presence) 3.) Your app is registered with Facebook. It is all done on the Facebook developer website under Settings.
To emulate the friend picker by hand inside my app I did the following:
Create a tab activity that shows two fragments. Each fragment shows a list. One fragment for available friend (/me/friends) and another for invitable friends (/me/invitable_friends). Use the same fragment code to render both tabs.
Create an AsyncTask that will get the friend data from Facebook. Once that data is loaded, toss it to the adapter which will render the values to the screen.
Details
The AsynchTask
private class DownloadFacebookFriendsTask extends AsyncTask<FacebookFriend.Type, Boolean, Boolean> {
private final String TAG = DownloadFacebookFriendsTask.class.getSimpleName();
GraphObject graphObject;
ArrayList<FacebookFriend> myList = new ArrayList<FacebookFriend>();
#Override
protected Boolean doInBackground(FacebookFriend.Type... pickType) {
//
// Determine Type
//
String facebookRequest;
if (pickType[0] == FacebookFriend.Type.AVAILABLE) {
facebookRequest = "/me/friends";
} else {
facebookRequest = "/me/invitable_friends";
}
//
// Launch Facebook request and WAIT.
//
new Request(
Session.getActiveSession(),
facebookRequest,
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if (error != null && response != null) {
Log.e(TAG, error.toString());
} else {
graphObject = response.getGraphObject();
}
}
}
).executeAndWait();
//
// Process Facebook response
//
//
if (graphObject == null) {
return false;
}
int numberOfRecords = 0;
JSONArray dataArray = (JSONArray) graphObject.getProperty("data");
if (dataArray.length() > 0) {
// Ensure the user has at least one friend ...
for (int i = 0; i < dataArray.length(); i++) {
JSONObject jsonObject = dataArray.optJSONObject(i);
FacebookFriend facebookFriend = new FacebookFriend(jsonObject, pickType[0]);
if (facebookFriend.isValid()) {
numberOfRecords++;
myList.add(facebookFriend);
}
}
}
// Make sure there are records to process
if (numberOfRecords > 0){
return true;
} else {
return false;
}
}
#Override
protected void onProgressUpdate(Boolean... booleans) {
// No need to update this, wait until the whole thread finishes.
}
#Override
protected void onPostExecute(Boolean result) {
if (result) {
/*
User the array "myList" to create the adapter which will control showing items in the list.
*/
} else {
Log.i(TAG, "Facebook Thread unable to Get/Parse friend data. Type = " + pickType);
}
}
}
The FacebookFriend class I created
public class FacebookFriend {
String facebookId;
String name;
String pictureUrl;
boolean invitable;
boolean available;
boolean isValid;
public enum Type {AVAILABLE, INVITABLE};
public FacebookFriend(JSONObject jsonObject, Type type) {
//
//Parse the Facebook Data from the JSON object.
//
try {
if (type == Type.INVITABLE) {
//parse /me/invitable_friend
this.facebookId = jsonObject.getString("id");
this.name = jsonObject.getString("name");
// Handle the picture data.
JSONObject pictureJsonObject = jsonObject.getJSONObject("picture").getJSONObject("data");
boolean isSilhouette = pictureJsonObject.getBoolean("is_silhouette");
if (!isSilhouette) {
this.pictureUrl = pictureJsonObject.getString("url");
} else {
this.pictureUrl = "";
}
this.invitable = true;
} else {
// Parse /me/friends
this.facebookId = jsonObject.getString("id");
this.name = jsonObject.getString("name");
this.available = true;
this.pictureUrl = "";
}
isValid = true;
} catch (JSONException e) {
Log.w("#", "Warnings - unable to process Facebook JSON: " + e.getLocalizedMessage());
}
}
}
Facebook has revised their policies now. You can’t get the whole friendlist anyway if your app does not have a Canvas implementation and if your app is not a game. Of course there’s also taggable_friends, but that one is for tagging only.
You will be able to pull the list of friends who have authorised the app only.
The apps that are using Graph API 1.0 will be working till April 30th, 2015 and after that it will be deprecated.
See the following to get more details on this:
User Friends
Facebook Application Development FAQ
In Swift 4.2 and Xcode 10.1:
If you want to get the friends list from Facebook, you need to submit your app for review in Facebook. See some of the Login Permissions:
Login Permissions
Here are the two steps:
1) First your app status is must be in Live
2) Get required permissions form Facebook.
1) Enable our app status live:
Go to the apps page and select your app
https://developers.facebook.com/apps/
Select status in the top right in Dashboard.
Submit privacy policy URL
Select category
Now our app is in Live status.
One step is completed.
2) Submit our app for review:
First send required requests.
Example: user_friends, user_videos, user_posts, etc.
Second, go to the Current Request page
Example: user_events
Submit all details
Like this submit for all requests (user_friends , user_events, user_videos, user_posts, etc.).
Finally submit your app for review.
If your review is accepted from Facebook's side, you are now eligible to read contacts, etc.
As Simon mentioned, this is not possible in the new Facebook API. Pure technically speaking you can do it via browser automation.
this is against Facebook policy, so depending on the country where you live, this may not be legal
you'll have to use your credentials / ask user for credentials and possibly store them (storing passwords even symmetrically encrypted is not a good idea)
when Facebook changes their API, you'll have to update the browser automation code as well (if you can't force updates of your application, you should put browser automation piece out as a webservice)
this is bypassing the OAuth concept
on the other hand, my feeling is that I'm owning my data including the list of my friends and Facebook shouldn't restrict me from accessing those via the API
Sample implementation using WatiN:
class FacebookUser
{
public string Name { get; set; }
public long Id { get; set; }
}
public IList<FacebookUser> GetFacebookFriends(string email, string password, int? maxTimeoutInMilliseconds)
{
var users = new List<FacebookUser>();
Settings.Instance.MakeNewIeInstanceVisible = false;
using (var browser = new IE("https://www.facebook.com"))
{
try
{
browser.TextField(Find.ByName("email")).Value = email;
browser.TextField(Find.ByName("pass")).Value = password;
browser.Form(Find.ById("login_form")).Submit();
browser.WaitForComplete();
}
catch (ElementNotFoundException)
{
// We're already logged in
}
browser.GoTo("https://www.facebook.com/friends");
var watch = new Stopwatch();
watch.Start();
Link previousLastLink = null;
while (maxTimeoutInMilliseconds.HasValue && watch.Elapsed.TotalMilliseconds < maxTimeoutInMilliseconds.Value)
{
var lastLink = browser.Links.Where(l => l.GetAttributeValue("data-hovercard") != null
&& l.GetAttributeValue("data-hovercard").Contains("user.php")
&& l.Text != null
).LastOrDefault();
if (lastLink == null || previousLastLink == lastLink)
{
break;
}
var ieElement = lastLink.NativeElement as IEElement;
if (ieElement != null)
{
var htmlElement = ieElement.AsHtmlElement;
htmlElement.scrollIntoView();
browser.WaitForComplete();
}
previousLastLink = lastLink;
}
var links = browser.Links.Where(l => l.GetAttributeValue("data-hovercard") != null
&& l.GetAttributeValue("data-hovercard").Contains("user.php")
&& l.Text != null
).ToList();
var idRegex = new Regex("id=(?<id>([0-9]+))");
foreach (var link in links)
{
string hovercard = link.GetAttributeValue("data-hovercard");
var match = idRegex.Match(hovercard);
long id = 0;
if (match.Success)
{
id = long.Parse(match.Groups["id"].Value);
}
users.Add(new FacebookUser
{
Name = link.Text,
Id = id
});
}
}
return users;
}
Prototype with implementation of this approach (using C#/WatiN) see https://github.com/svejdo1/ShadowApi. It is also allowing dynamic update of Facebook connector that is retrieving a list of your contacts.
Try /me/taggable_friends?limit=5000 using your JavaScript code
Or
try the Graph API:
https://graph.facebook.com/v2.3/user_id_here/taggable_friends?access_token=
If you are still struggling with this issue on a development mode.
Follow the same process as mentioned below:
create a test app of your main app,
create test users, automatically install app for test users and assign them 'user_friend' permission.
Add your test users as a friend with each other.
I followed the same process after going through alot of research and finally it worked.
In the Facebook SDK Graph API v2.0 or above, you must request the user_friends permission from each user in the time of Facebook login since user_friends is no longer included by default in every login; we have to add that.
Each user must grant the user_friends permission in order to appear in the response to /me/friends.
let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
fbLoginManager.loginBehavior = FBSDKLoginBehavior.web
fbLoginManager.logIn(withReadPermissions: ["email","user_friends","public_profile"], from: self) { (result, error) in
if (error == nil) {
let fbloginresult : FBSDKLoginManagerLoginResult = result!
if fbloginresult.grantedPermissions != nil {
if (fbloginresult.grantedPermissions.contains("email")) {
// Do the stuff
}
else {
}
}
else {
}
}
}
So at the time of Facebook login, it prompts with a screen which contain all the permissions:
If the user presses the Continue button, the permissions will be set. When you access the friends list using Graph API, your friends who logged into the application as above will be listed
if ((FBSDKAccessToken.current()) != nil) {
FBSDKGraphRequest(graphPath: "/me/friends", parameters: ["fields" : "id,name"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil) {
print(result!)
}
})
}
The output will contain the users who granted the user_friends permission at the time of login to your application through Facebook.
{
data = (
{
id = xxxxxxxxxx;
name = "xxxxxxxx";
}
);
paging = {
cursors = {
after = xxxxxx;
before = xxxxxxx;
};
};
summary = {
"total_count" = 8;
};
}
Related
I am using Flask at the backend and ganache for my blockchain project. I am also using Metamask. My smart contract is on the remix website. And here is my contract:
pragma solidity ^0.4.21;
contract Election{
struct Candidate {
uint voteCount;
string name;
}
struct voter {
bool authorized;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
modifier ownerOnly() {
require(msg.sender == owner);
_;
}
constructor(string _name) public {
owner = msg.sender;
electionName = _name;
}
function addCandidate(string _name) ownerOnly public {
candidates.push(Candidate(0, _name));
}
function getNumCandidates() public view returns(uint) {
return candidates.length;
}
function authorize(address _person) ownerOnly public {
voters[_person].authorized = true;
}
function vote(uint _voteIndex) public {
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorized);
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
candidates[_voteIndex].voteCount += 1;
totalVotes += 1;
}
function end() ownerOnly public {
selfdestruct(owner);
}
}
I am facing a problem with Vote function. When I run this whole contract after deploying on the remix website it is working fine. But when I am making transaction from my flask based app there is revert error even after having the voter in Voters list. So using metamask I want to make my transaction for voting. I deploy the contract with a default account.
w3 = Web3(HTTPProvider('http://127.0.0.1:7545'))
w3.eth.defaultAccount = w3.eth.accounts[0]
contract = w3.eth.contract(address=address, abi=data["abi"])
tx_hash = contract.functions.vote(candidate_index).transact()
w3.eth.wait_for_transaction_receipt(tx_hash)
Now I want to make transactions from the account which I choose in Metamask. Because the above method is not working. Is there any way or any example to do it? I also visited metamask documentation where the use of ethereum.request() is suggested. But I was unable to implement it in web3py.
Your code is correct. There are 2 require statements in vote. I think that is why you are getting the error. You can add custom message to require functions so you can see which one is causing the error:
require(!voters[msg.sender].voted,"You already voted");
require(voters[msg.sender].authorized,"You are not authorized");
I believe since you already call vote with your default account, you are not passing the first require statement.
I'm new to React, I've written a Django API endpoint to perform some kind of sync. I want this API to be called from a React page, on clicking a TextLink - say Sync Now, how I should go about doing it? Based on the response from the API(200 or 400/500), I want to display Sync failed, on successful sync, I want to show Sync Now again, but with another text Last Sync Time: DateTime(for this I've added a key in my Django model), how can I use this as well.
Also, I've a followup, say instead of Sync Now and Synced we have another state Syncing, which is there until a success or failure is returned, Is polling from the server is a good option, or is there any other way. I know of websockets but not sure which can be used efficiently.
I've been stuck here from 3 days with no real progress. Anything will help. Thanks in advance.
I'd do something like that:
// Store last sync date in state with default value null
const [lastSyncDate, setLastSyncDate] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const handleApiCall = async () => {
try {
setIsLoading(true);
const response = await fetch('api.url');
if (response.status === 200) {
const currentDate = Date.now().toString();
setLastSyncDate(currentDate);
}
}
catch(error) {
// handle error here
}
finally {
setIsLoading(false);
}
}
if (isLoading) {
return 'Loading...';
}
return (
<div onClick={() => handleApiCall()}>
{lastSyncDate !== null ? `lastSynced: ${lastSyncDate}` : "Sync now"}
</div>
)
I have some issue about microsoft-botframework.
I made a chatbot using microsoft-botframework Python SDK.
So I deploy it as Webchat , I attached this on my website, Wordpress.
However, the Auto-scroll on card doesn't work.
When new card appear, Auto scroll doesn't working.
So this is my critical Issue and I really want someone help.
Thank you.
This is a known issue in WebChat. There is a work around where you can use a custom WebChat Activity Middleware that scrolls the last message into view when the chat window receives a new message. Take a look at the code snippet below.
const store = window.WebChat.createStore(
{},
({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/POST_ACTIVITY_FULFILLED') {
document.querySelector('ul[role="list"]').lastChild.scrollIntoView({behavior: 'smooth', block: 'start'});
}
return next(action);
}
);
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
store
}, document.getElementById('webchat'));
Hope this helps!
This worked for me, but I have a strange usage (directline embedded in a Sharepoint page, ie. not absolute/fixed position):
const store = window.WebChat.createStore(
{},
({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/POST_ACTIVITY_FULFILLED') {
var scrollDiv = $('ul[role="list"]').first().parent();
scrollDiv.scrollTop(scrollDiv[0].scrollHeight);
}
return next(action);
}
);
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({
token: 'mySecret'
}),
store,
...
Hope it can help someone else.
If you don't want to use jQuery, replace the 2 scrollDiv lines with:
var scrollDiv = document.querySelector('ul[role="list"]').parentElement;
scrollDiv.scrollTop = scrollDiv.scrollHeight;
Firebase-auth.js (3.5.2)
Appengine Python
Google Cloud Endpoints
My login process works great most of the time, but occasionally, a duplicate user account is created. They are timestamped milliseconds apart. I have attempted to put checks in the JS to prevent the duplicates, but I'm still occasionally seeing duplicates.
I opened a support ticket with firebase. They instructed me to post the question here instead.
The clientConnect endpoint is where the user is created if it does not already exist.
Is there a better way to do this?
// We are getting duplicate user registrations sometimes because of the way firebase spams refreshes on the page.
// Trying to isolate so only one request happens at a time.
var checkAuthenticationInProgress = false;
firebase.auth().onAuthStateChanged(function(user) {
console.log('app.js - onAuthStateChanged')
if (user) {
console.log('onAuthStateChanged - user found')
user.getToken().then(function(idToken) {
userIdToken = idToken;
$http.defaults.headers.common['X-UETOPIA-Auth'] = 'Bearer ' + userIdToken;
checkEndpointsAuthentication();
}}}
checkEndpointsAuthentication = function() {
if (userIdToken) {
if (!checkAuthenticationInProgress) {
checkAuthenticationInProgress = true;
endpoints.post('users', 'clientConnect', {})
.then(function(response) {
// DONE!
console.log(response);
if (response.refresh_token) {
console.log('Firebase Unauth - REFRESHING');
console.log(firebase.auth());
}
setTimeout(resetCheckAuthenticationInProgress(), 2000);
}, function() {
// ERROR!
console.log('error');
setTimeout(resetCheckAuthenticationInProgress(), 2000);
});
} else {
console.log('already checking auth - retrying');
setTimeout(checkEndpointsAuthentication(), 2000);
}
} else {
console.log('no userIdToken found - skipping');
}
};
// I think it is being reset too quickly in some cases.
resetCheckAuthenticationInProgress = function() {
checkAuthenticationInProgress = false;
}
Your problem seems to be that the onAuthStateChanged callback is called once the user logs in, and then again on the page redirected to by signInSuccessUrl (the same page, in this case).
You can either use the signInSuccess callback (called before the final refresh), or not use the Firebase UI library.
Keep in mind that if you are trying to capture only the initial sign-up, then both onAuthStateChanged and signInSuccess will trigger on every login. You'll have to check that the user was just created, or do some deduplication on your server to prevent duplicate users on future logins.
Within my Python webapp for the Microsoft Botframework, I want to reply to a message with a REST API call to /bot/v1.0/messages.
When experimenting with the emulator on my local machine, I realized that the minimum payload for the REST call is something like:
{
"text": "Hello, Hello!",
"from": {
"address": "MyBot"
},
"channelConversationId": "ConvId"
}
where "ConvId" is the id given by my local emulator in the original message (Note, that I have to send channelConversationId not conversationId).
Obviously, this is not enough for the live bot connector site. But what is a (minimum) example for replying to a message with the REST API call /bot/v1.0/messages?
I've tested different payload data, for example with attributes from, to, channelConversationId, textand language as indicated in the docs. But usually I get a 500 error:
{
"error": {
"message": "Expression evaluation failed. Object reference not set to an instance of an object.",
"code": "ServiceError"
}
}
When I tried to send back the original message, just with to and from swapped, I got a different 500 error:
{
"error": {
"code": "ServiceError",
"message": "*Sorry, Web Chat is having a problem responding right now.*",
"statusCode": 500
}
}
The minimum payload for an inline reply (returned as the response) is:
{ "text": "Hello, Hello!" }
If you're posting a reply to out of band using a POST to /bot/v1.0/messages then you're correct you need a little more. Here's what I do in the Node version of the Bot Builder SDK:
// Post an additional reply
reply.from = ses.message.to;
reply.to = ses.message.replyTo ? ses.message.replyTo : ses.message.from;
reply.replyToMessageId = ses.message.id;
reply.conversationId = ses.message.conversationId;
reply.channelConversationId = ses.message.channelConversationId;
reply.channelMessageId = ses.message.channelMessageId;
reply.participants = ses.message.participants;
reply.totalParticipants = ses.message.totalParticipants;
this.emit('reply', reply);
post(this.options, '/bot/v1.0/messages', reply, (err) => {
if (err) {
this.emit('error', err);
}
});
Sending a reply to an existing conversation is a little complicated because you have to include all of the routing bits needed to get it back to the source conversation. Starting a new conversation is significantly easier:
// Start a new conversation
reply.from = ses.message.from;
reply.to = ses.message.to;
this.emit('send', reply);
post(this.options, '/bot/v1.0/messages', reply, (err) => {
if (err) {
this.emit('error', err);
}
});
Both examples assume that reply.text & reply.language has already been set.
Meanwhile, the answer has been posted to a GitHub issue, quoting wiltodelta
Experimentally found necessary parameters for Slack, Skype, Telegram: ... ChannelConversationId only necessary Slack, otherwise the message will be added #userAddress.
Message message = new Message
{
ChannelConversationId = channelConversationId,
From = new ChannelAccount
{
ChannelId = channelId,
Address = botAddress,
IsBot = true
},
To = new ChannelAccount
{
ChannelId = channelId,
Address = userAddress
},
Text = text
};
Especially, the attributes replyToMessageId and channelConversationId (earlier mentioned) are not necessary: They refer to the last seen message in the conversation and thus will change during a conversation.