在Rails中检查Facebook用户权限 [英] Checking for Facebook user permissions in Rails

查看:77
本文介绍了在Rails中检查Facebook用户权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Rails应用中测试Facebook用户的授予权限.

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

这是我检查权限时Facebook返回的内容:

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}]}

因此,我想检查是否存在publish_actions权限.这是我尝试过的:

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

我认为问题可能出在这行上,因为似乎方法返回false并发布了默认消息.

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?

推荐答案

发现了问题.这有效:

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

这篇关于在Rails中检查Facebook用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆