Skip to content
Russell Sim edited this page Jun 7, 2017 · 2 revisions

Here is an example script of how to use the OAuth2 gem with Zendesk's implementation.

require 'zendesk_api'
require 'oauth2'
 
client = OAuth2::Client.new('{my client unique identifier}',
  '{my client secret}',
  site: 'https://{subdomain}.zendesk.com',
  token_url: "/oauth/tokens",
  authorize_url: "/oauth/authorizations/new")
 
 
# The redirect_uri must *exactly* match one of those entered in the client configuration
auth_url = client.auth_code.authorize_url(:redirect_uri => 'https://example.com/', scope: "read write")
# Once you click approve you will be redirected to https://sample.herokuapp.com/?code={code}
# token = client.auth_code.get_token('{code}', :redirect_uri => 'https://zendesk-wall.herokuapp.com/')
 
auth_url = client.implicit.authorize_url(:redirect_uri => 'https://example.com/', scope: "read")
# Once approved, you will be redirected to https://sample.herokuapp.com/#access_token={token}
# That token can only be accessed by client-side javascript and will never be sent to the server
 
# Password strategy
token = client.password.get_token('{zendesk username}', '{zendesk password}', scope: "read write")
 
# Client credentials strategy
token = client.client_credentials.get_token(user_id: {zendesk user_id}, scope: "read write")
 
# Sample API request
client = ZendeskAPI::Client.new do |c|
  c.access_token = token.token
  c.url = "https://{subdomain}.zendesk.com/api/v2"
 
  require 'logger'
  c.logger = Logger.new(STDOUT)
end
 
puts client.current_user
Clone this wiki locally