V2 API Oauth2 Examples

Keith Nash
Keith Nash
  • Updated

Ruby example using the oauth2 gem (gem install oauth2)

require 'oauth2'

client_id = 'CLIENT_ID_PROVIDED_BY_WHIPLASH'
client_secret = 'CLIENT_SECRET_PROVIDED_BY_WHIPLASH'
client_scope = 'user_manage'
api_url = '<https://www.getwhiplash.com>'
# the redirect url should be whatever redirect url you provided to us when you signed up for Oauth
redir = '<https://hookbin.com/bin/vLNL1rj9>'

client = OAuth2::Client.new(client_id, client_secret, site: api_url)

auth_url = client.auth_code.authorize_url(:redirect_uri => redir, scope: client_scope)

puts " go to #{auth_url} to authenticate"
puts "after authenticating, enter the auth code from the 'code' param in the redirect url:"
code = gets.chomp

token = client.auth_code.get_token(code, :redirect_uri => redir)

response = token.get("#{api_url}/api/v2/orders/count", {})

puts "response code is #{response.status}"
puts "response body is #{JSON.parse(response.body)}"

Python example using OAuth2Session (pip install requests_oauthlib)

#!/usr/bin/env python3
# coding=utf-8

import http.client as http_client
import logging
from requests_oauthlib import OAuth2Session

http_client.HTTPConnection.debuglevel = 1

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

# Credentials
client_id = 'CLIENT_ID_PROVIDED_BY_WHIPLASH'
client_secret = 'CLIENT_SECRET_PROVIDED_BY_WHIPLASH'
authorization_base_url = '<https://www.getwhiplash.com/oauth/authorize>'
token_url = '<https://www.getwhiplash.com/oauth/token>'
# the redirect url should be whatever redirect url you provided to us when you signed up for Oauth
redirect_uri = '<https://hookbin.com/bin/vLNL1rj9>'
scope='user_manage'

whiplash = OAuth2Session(
    client_id=client_id,
    scope=scope,
    redirect_uri=redirect_uri
)

authorization_url, state = whiplash.authorization_url(authorization_base_url)
print('Please go here and authorize: {url}'.format(url=authorization_url))
authorization_code = input('Authorization code: ')

whiplash.fetch_token(
    token_url=token_url,
    code=authorization_code,
    client_id=client_id,
    client_secret=client_secret
)

r = whiplash.get('<https://www.getwhiplash.com/api/v2/orders/count>',
                 params={},
                 headers={})

print('\\n Status code: {}'.format(r.status_code))
print('Content: {}'.format(r.content))

 

Related Questions:

Where can I see a Ruby example using the oauth2 gem?

Where can I see a Python example using OAuth2Session?

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request