繁体   English   中英

Rails-Slack API OAuth访问-invalid_client_id

[英]Rails - Slack API OAuth Access - invalid_client_id

我正在为我的Ruby on Rails应用程序构建Slack集成,并且当用户单击“添加到Slack”按钮时,我试图从Slack应用程序的Slack API获取access_token。

从邮递员,我可以成功发布以下内容:

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

但是,在Rails中,无论调用API的方式如何,我总是会收到带有invalid_client_id的响应。 我检查了我的ID是否正确(很多)并尝试重新生成它,但是由于邮递员的成功,我不认为这是问题。

在我的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响应。

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

更新:

我刚刚发现,许多(也许全部)Slack API不接受JSON格式的主体(当看到它们以JSON发送响应时,这似乎很疯狂。

确保根据您的要求使用x-www-form-urlencoded格式主体,否则它将无法正常工作。

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

我使用oauth2 gem进行授权。 因此,我可以通过阅读松弛文档并在控制器中使用oauth2来使其工作:

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    

路线文件:

get    '/authorize', to: 'oauth#authorize'                                       
get    '/oauth/callback', to: 'oauth#authorize_callback'  

不要忘记在api.slack.com上的Oauth设置中设置回调URL,您可以看到,我使用localhost进行测试。

暂无
暂无

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

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