User Unfollow

api

#1

So, when you follow or unfollow a user, you receive the user object.

Inside this object you have “following” which is whether or not the current user is following that user.

When I send a post req too “friendships/create” this returns “following:true” as expected.
But now, if the user unfollows, for some reason “friendships/destroy” still returns “following:true” even though on twitter it says you unfollowed.

Here’s a short video that shows what happens: https://6.lithi.io/AMYUR.mp4

Very strange, and i think this a problem on twitters end not mine

Any ideas though?

Thanks


#2

Not a major issue, i think twitter should change this though.

For now, after the follow/unfollow I made one more request to get the users information and returned that instead, solving my problem… but thats one extra api request, not very efficient considering the follow/unfollow returns the user object!


#3

Hi Same, can you elaborate on how you got yours to work. I’m having issues with mine although I think I’m no were near as experienced as you. Basically the iteration/loop in the top part of code is to check index in table of displayed users to unfollow…and then the request to unfollow is executed below.

I get an Optional Error every time my code below runs:
Error: “Error: Optional(Error Domain=TWTRNetworkingErrorDomain Code=-1011 “Request failed: unauthorized (401)” UserInfo={NSLocalizedFailureReason=, TWTRNetworkingStatusCode=401, NSErrorFailingURLKey=https://api.twitter.com/1.1/friendships/destroy.json, NSLocalizedDescription=Request failed: unauthorized (401)})”

func unfollowUser(user: User)
{
    var index = 0
    for i in 0..<users.count
    {
        if users[i] === user
        {
            index = i
            self.indexedUserToDelete = index
            print(user.screenName)
            print(user.idString)
            self.unfollowParam = user.idString //as String!
            //self.unfollowParamTwo = user.screenName as String?
            break
        }
    }
    let doUnFollow = self.client.urlRequest(withMethod: "POST", url: "https://api.twitter.com/1.1/friendships/destroy.json", parameters: ["user_id": self.unfollowParam!], error: &self.clientError)
    self.client.sendTwitterRequest(doUnFollow)
    { (response, data, connectionError) -> Void in
        if (connectionError == nil)
        {
            do
            {
                if let json: Any = try JSONSerialization.jsonObject(with: data!, options: [])
                {
                    if let item: Any? = json as? [String: AnyObject]
                    {
                        print(item)
                        //Sends for the unfollowUser(user: User) to delete item from Table
                        self.deleteUserFromRow(user: user)
                    }
                }
            }
            catch let error
            {
                print(error)
            }
            
        }
        else
        {
            print("Error: \(connectionError)")
        }
        
        
    }
}

#4

Ooops! Sorry, I’m coding in Swift. I should have checked first.


#5

You code reminded me why I never got into Swift! lol,

But from reading your error, it seems that you’re having an issue because you’re not authorized as the user, as far as I know, all of twitters endpoints require authorization…

But also, after taking a read through your code again, I’m pretty sure those endpoints were deprecated?


#6

I believe friendships/destroy is still valid.