通过 OAuth 将 Rails 3 连接到 Salesforce/任何其他应用程序

发布于 2025-01-08 12:45:08 字数 1607 浏览 5 评论 0原文

有人通过 Rails 3 App 通过 oauth 连接到 Salesforce 吗?您能否发布执行相同操作的代码。我正在尝试相同,但我收到一些错误,下面是我的代码

      def oauth_client
        consumer_key = '....'
        consumer_secret = '....'
        oauth_options = {
          :site               => 'https://login.salesforce.com',
          :scheme             => :body,
          :request_token_path => '/_nc_external/system/security/oauth/RequestTokenHandler',
          :authorize_path     => '/setup/secur/RemoteAccessAuthorizationPage.apexp',
          :access_token_path  => '/_nc_external/system/security/oauth/AccessTokenHandler',
        }
        OAuth::Consumer.new consumer_key, consumer_secret, oauth_options
      end

      def oauth_redirect_uri
        uri = URI.parse(request.url)
        uri.path = '/sfdc/oauth_callback'
        uri.query = nil
        #  uri = "http://localhost:3000/sfdc/oauth_callback"
        uri.to_s
      end

      def oauth_connect
        consumer_key    = '...' # from SalesForce
        consumer = oauth_client
        request_t = consumer.get_request_token
        redirect_to request_t.authorize_url(
        :redirect_uri => oauth_redirect_uri,
        :oauth_consumer_key => consumer_key
        )
      end

      def oauth_callback  
        access = request_t.get_access_token :oauth_verifier => params[:oauth_verifier]
        p access
        render :text => access.token
      end

错误未定义方法 get_access_token for #。这里的request变量是nil。我该如何取回它?

Has anybody connected to Salesforce through Rails 3 App via oauth? Could you please post code for doing same. I am trying to same but I get some error below is my code

      def oauth_client
        consumer_key = '....'
        consumer_secret = '....'
        oauth_options = {
          :site               => 'https://login.salesforce.com',
          :scheme             => :body,
          :request_token_path => '/_nc_external/system/security/oauth/RequestTokenHandler',
          :authorize_path     => '/setup/secur/RemoteAccessAuthorizationPage.apexp',
          :access_token_path  => '/_nc_external/system/security/oauth/AccessTokenHandler',
        }
        OAuth::Consumer.new consumer_key, consumer_secret, oauth_options
      end

      def oauth_redirect_uri
        uri = URI.parse(request.url)
        uri.path = '/sfdc/oauth_callback'
        uri.query = nil
        #  uri = "http://localhost:3000/sfdc/oauth_callback"
        uri.to_s
      end

      def oauth_connect
        consumer_key    = '...' # from SalesForce
        consumer = oauth_client
        request_t = consumer.get_request_token
        redirect_to request_t.authorize_url(
        :redirect_uri => oauth_redirect_uri,
        :oauth_consumer_key => consumer_key
        )
      end

      def oauth_callback  
        access = request_t.get_access_token :oauth_verifier => params[:oauth_verifier]
        p access
        render :text => access.token
      end

Error undefined method get_access_token for #<ActionDispatch::Request:0x12b79f370>. the request variable is nil here. How do I get it back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2025-01-15 12:45:08

rforce gem 有很多我粘贴在下面的示例。然而,您可能只想使用 rforce 而不是自己动手。

def init_server(url)
  @url = URI.parse(url)

  if (@oauth)
    consumer = OAuth::Consumer.new \
      @oauth[:consumer_key],
      @oauth[:consumer_secret],
      {
        :site => url,
        :proxy => @proxy
      }

    consumer.http.set_debug_output $stderr if show_debug

    @server  = OAuth::AccessToken.new \
      consumer,
      @oauth[:access_token],
      @oauth[:access_secret]

    class << @server
      alias_method :post2, :post
    end
  else
    @server = Net::HTTP.Proxy(@proxy).new(@url.host, @url.port)
    @server.use_ssl = @url.scheme == 'https'
    @server.verify_mode = OpenSSL::SSL::VERIFY_NONE

    # run ruby with -d or env variable SHOWSOAP=true to see SOAP wiredumps.
    @server.set_debug_output $stderr if show_debug
  end
end

The rforce gem has quite a bit of an example that I pasted below. However you might just want to use rforce instead of rolling your own.

def init_server(url)
  @url = URI.parse(url)

  if (@oauth)
    consumer = OAuth::Consumer.new \
      @oauth[:consumer_key],
      @oauth[:consumer_secret],
      {
        :site => url,
        :proxy => @proxy
      }

    consumer.http.set_debug_output $stderr if show_debug

    @server  = OAuth::AccessToken.new \
      consumer,
      @oauth[:access_token],
      @oauth[:access_secret]

    class << @server
      alias_method :post2, :post
    end
  else
    @server = Net::HTTP.Proxy(@proxy).new(@url.host, @url.port)
    @server.use_ssl = @url.scheme == 'https'
    @server.verify_mode = OpenSSL::SSL::VERIFY_NONE

    # run ruby with -d or env variable SHOWSOAP=true to see SOAP wiredumps.
    @server.set_debug_output $stderr if show_debug
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文