Trying to create a VK provider but keep getting client_secret is undefined error
I have created an authentication blueprint for Vkontakte using the Custom provider template as shown in the Flask-Dance documentation. However, I keep getting the error "oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client) client_secret is undefined" whenever I run my flask app. I have no idea how to fix this error.
Without seeing the actual code we won't be able to help you.
Here's the code I wrote:
from flask import Flask, render_template, redirect, url_for
from flask_dance.consumer import OAuth2ConsumerBlueprint
from flask_dance.consumer.requests import OAuth2Session
app = Flask(__name__)
app.config['SECRET_KEY'] = 'thisissupposedtobemysecretsecretkey'
app.config['VK_OAUTH_CLIENT_ID'] = 'CLIENT-ID'
app.config['VK_OAUTH_CLIENT_SECRET'] = 'CLIENT-SECRET'
vkBlueprint = OAuth2ConsumerBlueprint(
"oauth-vk", __name__,
scope=["friends"],
#api_version="5.122",
base_url="https://api.vk.com/method/",
token_url="https://oauth.vk.com/access_token",
authorization_url="https://oauth.vk.com/authorize",
authorization_url_params= {"v":"5.122"},
redirect_url = "/dashboard"
)
vkBlueprint.from_config["client_id"] = 'VK_OAUTH_CLIENT_ID'
vkBlueprint.from_config["client_secret"] = 'VK_OAUTH_CLIENT_SECRET'
app.register_blueprint(vkBlueprint, url_prefix="/vk_login")
@app.route("/")
@app.route("/home")
def home():
return render_template("index.html")
@app.route("/dashboard")
def dashboard():
#breakpoint()
if not vkBlueprint.session.authorized:
#breakpoint()
return redirect(url_for('oauth-vk.login'))
resp = vkBlueprint.session.get("friends.get")
if resp.ok:
return f"Here's the content of my response: {resp.content}"
return "Authentication Failed!"
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=2020)
Below is the index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Oauth-VK</title>
</head>
<body>
<div>
<a href="{{ url_for('dashboard') }}">Dashboard</a>
</div>
</body>
</html>
Finally, this the error i got:
Traceback (most recent call last): File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\flask_dance\consumer\oauth2.py", line 260, in authorized token = self.session.fetch_token( File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\requests_oauthlib\oauth2_session.py", line 358, in fetch_token self._client.parse_request_body_response(r.text, scope=self.scope) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 421, in parse_request_body_response self.token = parse_token_response(body, scope=scope) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 431, in parse_token_response validate_token_parameters(params) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 438, in validate_token_parameters raise_from_error(params.get('error'), params) File "D:\programming\Oauth-Flask-VK\env\Lib\site-packages\oauthlib\oauth2\rfc6749\errors.py", line 405, in raise_from_error raise cls(**kwargs) oauthlib.oauth2.rfc6749.errors.InvalidClientError: (invalid_client) client_secret is undefined
After running the pdb debugger on the code, I realised that the error arises from the requests-oauthlib library, specifically the fetch_token() from the OAuth2Session class. I understand Flask-dance was built on top this library.