Checking for Facebook user permissions in Rails

2020-07-29 00:47发布

问题:

I'm trying to test for granted permissions by Facebook users in a rails app.

This is what Facebook returns when I check for permissions:

#<HTTParty::Response:0x7f82dc963820 @parsed_response={"data"=>[{"installed"=>1, "status_update"=>1, "photo_upload"=>1, "video_upload"=>1, "offline_access"=>1, "email"=>1, "create_note"=>1, "share_item"=>1, "publish_stream"=>1, "publish_actions"=>1, "user_likes"=>1, "user_about_me"=>1}]}

So, I want to check for the publish_actions permission, which, is there. This is what I've tried:

def self.to_facebook(user, chapter, action)
    auth = Authorization.find_by_user_id(user)
    if self.user_has_granted_publish_actions?(auth)
      ret = HTTParty.post('https://graph.facebook.com/' + "#{auth.uid}" + '/bookstore:' + "#{action}" + '?access_token=' + "#{auth.token}" + '&chapter=http://samples.ogp.me/1403776044881')
    else
      message = URI.escape('Test message')
      ret = HTTParty.post('https://graph.facebook.com/' + "#{auth.uid}" + '/feed' + '?access_token=' + "#{auth.token}" + '&message=' + "#{message}")
    end
  end

  def self.user_has_granted_publish_actions?(authorization)
    auth = Authorization.find(authorization)
    ret = HTTParty.get('https://graph.facebook.com/' + "#{auth.uid}" + '/permissions' + '?access_token=' + "#{auth.token}")
    return true if ret.parsed_response['data'].include?('publish_actions') && ret.parsed_response['data']['publish_actions'] == 1
  end

I think the problem is probably on this line because it seems that method is returning false and the default message is being posted.

return true if ret.parsed_response['data'].include?('publish_actions') && ret.parsed_response['data']['publish_actions'] == 1

Any thoughts on what I'm doing wrong?

回答1:

Found the problem. This works:

return true if ret.parsed_response['data'][0].has_key?('publish_actions') && ret.parsed_response['data'][0]['publish_actions'] == 1