Skip to content

Instantly share code, notes, and snippets.

@steved
Created August 6, 2013 18:26
Show Gist options
  • Save steved/6167162 to your computer and use it in GitHub Desktop.
Save steved/6167162 to your computer and use it in GitHub Desktop.
Ruby OAuth2 Example for Zendesk
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment