requests / requests-oauthlib

OAuthlib support for Python-Requests!
https://requests-oauthlib.readthedocs.org/
ISC License
1.71k stars 422 forks source link

oauthlib.oauth2.rfc6749.errors.InvalidClientIdError: (invalid_request) Invalid client_id parameter value. #405

Open andyspeak opened 4 years ago

andyspeak commented 4 years ago

Hello. Ive been tearing my hair out over this for hours and have finally conceded that it may be a bug as there was someone 5 years ago (#203) with a similar issue on here that turned out to be a bug. The parameters are all fine, and i even tried with the other grant type offered by netatmo but it always returns to the same error of invalid client_id parameter value.

import json
from flask import Flask, render_template, request, redirect, session, url_for
from flask.json import jsonify
import os
from requests_oauthlib import OAuth2Session

app = Flask(__name__)

client_id = "xxxx"
client_secret = "xxxx"
scope = 'read_station'
#grant_type = 'authorization_code'
grant_type = 'password'
password = 'xxxx'
authurl = 'https://api.netatmo.com/oauth2/authorize?'
token_url = 'https://api.netatmo.com/oauth2/token'

bolzano = 'lat_ne=46.3&lon_ne=11.3&lat_sw=46.2&lon_sw=11.2'

@app.route('/')
def auth():
    redirect_uri = url_for('.redir', _external = True)
    oauth = OAuth2Session(client_id, redirect_uri = redirect_uri,
                          scope = scope)
    authorization_url, state = oauth.authorization_url(authurl)
    session['oauth_state'] = state
    return redirect(authorization_url)

@app.route('/redir', methods = ["GET","POST"])
def redir():
    code = request.args.get('code')
    oauth = OAuth2Session(client_id, state=session['oauth_state'])
    #token = oauth.fetch_token(token_url, client_id = client_id, client_secret=client_secret,
    #code = code,scope = 'read_station',authorization_response = request.url)
    token = oauth.fetch_token(token_url, client_id = client_id, client_secret=client_secret, username = 'xxxx',password = 'xxxx',scope = 'read_station', authorization_response = request.url)
    session['oauth_token'] = token
    return redirect(url_for('.profile'))

@app.route('/profile', methods = ["GET","POST"])
def profile():
    oauth = OAuth2Session(client_id, token=session['oauth_token'])
    return jsonify(oauth.get('https://api.netatmo.com/api/getpublicdata?'+bolzano+'&filter=false').json())

if __name__ == "__main__":
    os.environ['DEBUG'] = "1"
    os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = "1"
    app.secret_key = os.urandom(24)
    app.run(debug=True)

Please help!

ryanlovett commented 3 years ago

I saw this using Zoom's api which returned

{"reason":"Invalid request : Redirect URI mismatch.","error":"invalid_request"}

but this bubbled up through requests-oauthlib as oauthlib.oauth2.rfc6749.errors.InvalidClientIdError: (invalid_request) Invalid client_id parameter value.

carolinarsm commented 2 years ago

My 2 cents. Check passinginclude_client_id=True in the fetch_token call. There is some logic being triggered if you don't set that flag and when not passing the argument auth explicitly. It will create an requests.auth.HTTPBasicAuth(client_id, client_secret) for you and send it in the request.