jeudi 5 avril 2018

Ruby-Invalid Signature Error when trying to GET using a module that generates "oauth signature"

Hi I tried to get a oAuth"request_token" to use Nokia health api. https://developer.health.nokia.com/api Since it doesn't provide a client library for Ruby, I had to write code authentication steps. So I also tried to use this code snippet(https://gist.github.com/cheenu/1469815/d2a6c2a2adb7ed7e31179a8259273f115eb62784) which is fit for my project. But no luck!

Whenever I send request using GET, it always sends a response: "Invalid signature" {"status":0,"message":"Invalid signature ....

Is there a correct way to generate OAuth Signature using Ruby? Any help would be appreciated! Regards,


Here I attach the current sourcecode which generates invalid OAuth Signature.

require "cgi"
require 'openssl'
require 'base64'

# Consumer key, provided by Nokia when registering as a partner.
oauth_consumer_key = ENV['nokia_api_key']

# Callback, url encoded.
# This callback will be called after the user has authorized your account
# to access his data (on step 2).
# User id for the user is provided as "userid=" url parameters to the callback.
oauth_callback = 'https://iaura.herokuapp.com/get_auth_token'
# Random string (should be different for every request).
oauth_nonce = rand(10 ** 30).to_s.rjust(30,'0')

# OAuth signature method. Should always be equal to HMAC-SHA1
oauth_signature_method = 'HMAC-SHA1'

# Current date as unix epoch
oauth_timestamp = Time.now.to_i.to_s

# oAuth version. Should always be equal to 1.0
oauth_version = '1.0'

url =  'https://developer.health.nokia.com/account/request_token'
parameters = 'oauth_callback=' + oauth_callback +
             '&oauth_consumer_key=' + oauth_consumer_key +
             '&oauth_nonce=' + oauth_nonce +
             '&oauth_signature_method=' + oauth_signature_method +
             '&oauth_timestamp=' + oauth_timestamp +
             '&oauth_version=' + oauth_version

base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(parameters)
secret_key = ENV['nokia_api_secret'] + '&' # Add & at the end of secret_key
oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1',secret_key, base_string)}"))
request_url = url + '?' + parameters + '&oauth_signature=' + oauth_signature

Aucun commentaire:

Enregistrer un commentaire