Rails - Slack API OAuth访问 - invalid_client_id

时间:2017-01-25 14:21:31

标签: ruby-on-rails ruby oauth-2.0 ruby-on-rails-5 slack-api

我正在为我的Ruby on Rails应用程序构建Slack集成,并且当用户单击Add to Slack按钮时,我试图从Slack API获取一个access_token。

从Postman,我可以成功发布以下内容:

https://slack.com/api/oauth.access?client_id=idgoes.here&client_secret=secretgoeshere&code=12345&pretty=1

但是,在Rails中,我总是会收到 invalid_client_id 的回复,无论我调用API的方式如何。我已经检查了我的身份证明是正确的(很多)并尝试重新生成它,但由于邮递员的成功,我不认为这是问题。

在我的get_oauth_access_token方法中,我尝试了以下实现:

1

rc = JSON.parse(HTTP.post('https://slack.com/api/oauth.access',
                           params: {
                           client_id: 'idgoes.here',
                           client_secret: 'secretgoeshere',
                           code: '12345'
                }))

2

response = Excon.post('https://slack.com/api/oauth.access',
                         headers: { 'Content-Type' => 'application/json; charset=utf-8' },
                         user: client_id, password: client_secret,
                         body: oauth_request_body.to_json)

我尝试的任何实现总是会得到invalid_client_id响应。

我知道它可能与环境配置有关,但我不确定什么对调试有帮助,所以请让我知道我可以分享的其他信息。我在localhost上运行。

更新

我刚刚发现许多(可能全部)Slack API不接受JSON格式体(当它们在JSON中发送响应时看起来很疯狂。

请确保在您的请求中使用x-www-form-urlencoded格式正文,否则将无法正常使用。

"Content-Type" => "application/x-www-form-urlencoded"

1 个答案:

答案 0 :(得分:2)

我使用oauth2 gem进行授权。所以我能够通过阅读slack documentation并在我的控制器中使用oauth2来实现这一点:

o <- data.frame(id = 1:2, country = c("US", "Mexico"), 
           start = c(1972, 1982), end= c(1975, 1986)) 
lst <- Map(`:`, o$start, o$end)
res1 <- cbind(o[rep(seq_len(nrow(o)), lengths(lst)), 1:2], year = unlist(lst))
row.names(res1) <- NULL

路由文件:

class OauthController < ApplicationController
  def authorize                                                                   
    options = {                                                                   
      site: 'https://slack.com/oauth/authorize'                                  
    }                                                                             
    client ||= OAuth2::Client.new(                                                
      'client-id',                                                 
      'client-secret',                                         
      options                                                                     
    )                                                                             
    params = {                                                                    
      scope: 'incoming-webhook, commands',                                        
      redirect_uri: 'https://localhost:3000/oauth/callback'                       
    }                                                                             
    redirect_to client.auth_code.authorize_url(params)                            
  end                                                                             

  def authorize_callback                                                          
    puts params["code"] 
    redirect_to root_url                                                              
  end 
end    

不要忘记在api.slack.com上的Oauth设置中设置您的回调网址,我使用localhost进行测试,如您所见。