namaggarwal / splitwise

Python SDK for Splitwise
MIT License
184 stars 46 forks source link

Authentication Error - Please check your request #55

Closed ManuelBanza closed 3 years ago

ManuelBanza commented 4 years ago

Hi,

I am having some problems with the authentication:

import pandas as pd from splitwise import Splitwise from flask import request

key and secret below

s = Splitwise("xxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxxxxxxx")

redirect_uri = "https://secure.splitwise.com/oauth/authorize" url, state = s.getOAuth2AuthorizeURL(redirect_uri) access_token = s.getOAuth2AccessToken(code, redirect_uri)

but I get the error: SplitwiseBadRequestException: Please check your request

Can anyone help me understand what I missed?

Thanks!

namaggarwal commented 4 years ago

Hi,

Apologies for late reply. Redirect_uri should be the URI of your app and should be whitelisted in Splitwise portal.

namaggarwal commented 3 years ago

Closing this issue as no update for more than a month

bpjobin commented 3 years ago

Redirect_uri should be the URI of your app and should be whitelisted in Splitwise portal.

Hi, can you be more specific? What do you mean by Splitwise portal?

namaggarwal commented 3 years ago

You would need to create an app here first https://secure.splitwise.com/apps

ManuelBanza commented 3 years ago

Hi @namaggarwal ,

Sorry but I still not understand. I already have created an app an it is call repo.

What should I replace in the code?

I also tried what is written in the READ.ME but is does not work:

from splitwise import Splitwise
from flask import request

sObj = Splitwise("<consumer key>","<consumer secret>")
url, secret = sObj.getAuthorizeURL()
#Store secret so you can retrieve it later
#redirect user to url

oauth_token    = request.args.get('oauth_token')
oauth_verifier = request.args.get('oauth_verifier')

sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
access_token = sObj.getAccessToken(oauth_token,session['secret'],oauth_verifier)

session['access_token'] = access_token

but I get the error:

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed an active HTTP request. Consult the documentation on testing for information about how to avoid this problem.

I just copy paste and change the consumer key.

ManuelBanza commented 3 years ago

Hi also tried the code form this link: https://splitwise.readthedocs.io/en/latest/user/authenticate.html#authenticate

But where do I get the oauth_token and oauth_verifier? In my app I only have this:

OAuth 1.0

Request Token URL https://secure.splitwise.com/oauth/request_token

Access Token URL https://secure.splitwise.com/oauth/access_token

Authorize URL https://secure.splitwise.com/oauth/authorize

I tried every way there is but it always misses something from the documentation. Can you help? Thanks

bpjobin commented 3 years ago

Hi @ManuelBanza,

Here's an example that should work on your end too. You need to specify your consumer key and secret as well as the Callback URL you put into your app. Since OAuth is a three way authentication service, it needs manual authentication. Thus, every steps must be done in the same session. Here, I use a web server (Flask) to get an authorization url which will give me a code (in the case of OAuth2) which is essentiel to get the access_token. Once you have an access_token, save it somewhere, it can be reused without having to do the entire authentication process again. Screen Shot 2021-01-12 at 4 45 44 AM

I find the documentation example rather incomplete and would be happy to see one like that included. Cheers

from flask import Flask
from flask import session
from flask import url_for
from flask import request
from flask import redirect
from splitwise import Splitwise

CONSUMER_KEY = "your_consumer_key"
CONSUMER_SECRET = "your_consumer_secret"
REDIRECT_URI = "http://localhost:5000/callback"  # <-- Needs to be the same as Callback URL in registered splitwise app.

app = Flask(__name__)  # make new flask instance
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/;kaspi8yo7ftdiu5ehlsv'

@app.route('/auth')
def auth():
    url, state = splitwise.getOAuth2AuthorizeURL(REDIRECT_URI)
    session['state'] = state
    return redirect(url)

@app.route('/callback')
def callback():
    code = request.args.get("code")
    access_token = splitwise.getOAuth2AccessToken(code, REDIRECT_URI)
    session['access_token'] = access_token

    return redirect(url_for('main'))

@app.route('/')
def main():
    access_token = session.get("access_token")
    if access_token:
        splitwise.setOAuth2AccessToken(access_token)
        return ", ".join([user.getFirstName().title() for user in splitwise.getFriends()]) + " are your friends."

    return redirect("/auth")

if __name__ == '__main__':
    splitwise = Splitwise(CONSUMER_KEY, CONSUMER_SECRET)
    app.run()