Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create razor-pay-server #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions oura/credentials-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
"CLIENT_ID" : "",
"CLIENT_SECRET" : "",
"ACCESS_TOKEN" : ""
},

"razorpay" : {
"CLIENT_ID" : "",
"CLIENT_SECRET" : "",
"ACCESS_TOKEN" : ""
}
}
71 changes: 71 additions & 0 deletions oura/razor-pay-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# go to https://cloud.ouraring.com/oauth/applications and create an application
# you will be provided with client_id and client_secret put them to config.py
# put ""http://localhost:65010/oura_auth" to redirect uri field

REDIRECT_URI = "http://localhost:65010/razorpay_auth"

import os
os.environ["FLASK_ENV"] = "development"

# configuration
import json
with open("credentials.json", "r") as file:
credentials = json.load(file)
razorpay_cr = credentials['oura']
CLIENT_SECRET = razorpay_cr['CLIENT_SECRET']
CLIENT_ID = razorpay_cr['CLIENT_ID']
# ACCESS_TOKEN = oura_cr['ACCESS_TOKEN']


from flask import Flask, abort, request
app = Flask(__name__)
@app.route('/')
def homepage():
text = '<a href="%s">Get access token!</a>'
return text % make_authorization_url()

def make_authorization_url():
# Generate a random string for the state parameter
# Save it for use later to prevent xsrf attacks
from uuid import uuid4
state = str(uuid4())
# save_created_state(state)
params = {"client_id": CLIENT_ID,
"response_type": "code",
"state": state,
"redirect_uri": REDIRECT_URI,
"duration": "temporary",
"scope": "email personal daily"} # all possible scopes
import urllib
url = "https://cloud.ouraring.com/oauth/authorize?" + urllib.parse.urlencode(params)
return url

import requests
import requests.auth
def get_token(code):
client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET)
post_data = {"grant_type": "authorization_code",
"code": code,
"redirect_uri": REDIRECT_URI}
response = requests.post("https://razorpay.com/oauth/token",
auth=client_auth,
data=post_data)
token_json = response.json()
return token_json["access_token"]

@app.route('/oura_auth')
def oura_redir():
error = request.args.get('error', '')
error = request.args.get('error_description', '')
if error:
return "Error: " + error
state = request.args.get('state', '')
##if not is_valid_state(state):
# Uh-oh, this request wasn't started by us!
## abort(403)
code = request.args.get('code')

return "got an access token! %s" % get_token(code)

if __name__ == '__main__':
app.run(debug=True, port=65010)