简体   繁体   English

在api请求中处理ENV变量

[英]Handling ENV Variables in api request

I am making a request to the facebook graph api and have saved my USER_ID and ACCESS_TOKEN in ENV variables, Im wondering if this is a best practice as this morning i am ecountering a URI error that i was not getting yesterday 我正在向facebook图表api发出请求,并在ENV变量中保存了我的USER_ID和ACCESS_TOKEN,我想知道这是否是最好的做法,因为今天早上我遇到了一个我昨天没有得到的URI错误

class FacebookFeed
  #Constants
  VANDALS_ID = ENV['VANDALS_FB_ID']
  FB_ACCESS_TOKEN = ENV['FACEBOOK_ACCESS_TOKEN']
  FACEBOOK_URL = 'https://graph.facebook.com/"#{VANDALS_ID}"/posts/?access_token="#{FB_ACCESS_TOKEN}"'

def get_feed
  uri = URI(FACEBOOK_URL)
  response = HTTParty.get(uri)
  results = JSON.parse(response.body)['data']
  puts results
end
end

So in the rails console I am just trying to get a response but am getting 所以在rails控制台中,我只是想得到一个响应,但我得到了

URI::InvalidURIError: bad URI(is not URI?): https://graph.facebook.com/"#{VANDALS_ID}"/posts/?access_token="#{FB_ACCESS_TOKEN}"

this is strange as this was working yesterday, but is there anything i am missing or is there a better way to store my User_ID and Access Token 这很奇怪,因为这在昨天工作,但有什么我缺少或有更好的方法来存储我的User_ID和访问令牌

Any help appreciated 任何帮助赞赏

UPDATE UPDATE

When doing a 'puts uri' this is returned 在做'put uri'时会返回

https://graph.facebook.com/%22%23%7BVANDALS_ID%7D%22/posts/?access_token=%22%23%7BFB_ACCESS_TOKEN%7D%22

which i assume is what is being sent as the GET request, because when i then do 'puts response' i get 我假设是作为GET请求发送的是什么,因为当我做“放回应”时我得到了

{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}

How do i construct the request correctly? 我如何正确构造请求? as what i am doing doesnt seem to be working 正如我所做的那样似乎并没有起作用

Thanks 谢谢

After talking to Rich on Skype, the problem was resolved with Sergey Kishenin 's comment: 在与Skype谈论Rich之后, Sergey Kishenin的评论解决了这个问题:

FACEBOOK_URL = "https://graph.facebook.com/#{VANDALS_ID}/posts/?access_token=#{FB_ACCESS_TOKEN} " FACEBOOK_URL = "https://graph.facebook.com/#{VANDALS_ID}/posts/?access_token=#{FB_ACCESS_TOKEN}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM