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

os.environ[
    'OAUTHLIB_INSECURE_TRANSPORT'] = '1'  # Because we don't have trusted certificate in the server(Only for demo)

app = Flask(__name__)

# This information is obtained upon registration of a new Application in WSO2 API Manager
# You can add application from here: https://localhost:9443/store/site/pages/application-add.jag

client_id = "kN6aHLmmI30UXtcwTCf1iAKaf5Ya"
client_secret = "imTCjzNXrRZVpbq0Xlx3LWr8ewwa"
scope = "sample_key"

host = "localhost"
port = 8243

authorization_base_url = 'https://{}:{}/authorize'.format(host, port)
token_url = 'https://{}:{}/token'.format(host, port)
callback_url = "http://{}:5000/callback".format(host)


@app.route("/")
def demo():
    """Step 1: User Authorization.

    Redirect the user/resource owner to the OAuth provider (i.e. WSO2 API Manager)
    using an URL with OAuth parameters.
    """
    wso2_apim = OAuth2Session(client_id, redirect_uri=callback_url, scope=scope)
    authorization_url, state = wso2_apim.authorization_url(authorization_base_url)

    # State is used to prevent CSRF, keep this for later.
    session['oauth_state'] = state
    return redirect(authorization_url)


# Step 2: User authorization, this happens on the provider. In the above redirect call user(you) will be redirect to
# WSO2 APIM request authorization page, Where user(you) will be asked whether to grant permission for the
# requested resource or reject the request. With User's(your) action. WSO2 APIM (Service Provider) will redirect the
# client back to it's designated callback URL.That redirect request will hit the following method.

@app.route("/callback", methods=["GET"])
def callback():
    """ Step 3: Retrieving an access token.

    The user has been redirected back from the provider to your registered
    callback URL. With this redirection comes an authorization code included
    in the redirect URL. We will use that to obtain an access token.
    """
    wso2_apim = OAuth2Session(client_id, state=session['oauth_state'], redirect_uri=callback_url)
    token = wso2_apim.fetch_token(token_url, client_secret=client_secret,
                                  authorization_response=request.url, verify=False)  # verify=False Because we don't
    # have trusted certificate in the server(Only for demo)

    # At this point you can fetch protected resources but lets save
    # the token and show how this is done from a persisted token
    # in /profile.
    session['oauth_token'] = token
    return redirect(url_for('.profile'))


@app.route("/profile", methods=["GET"])
def profile():
    """Fetching a protected resource using an OAuth 2 token.
    """
    print("Done!")
    print("Client access key is = {}".format(session['oauth_token']))
    return jsonify("{{'done': True, 'access_token': {}}}".format(session['oauth_token']))


if __name__ == "__main__":
    # This allows us to use a plain HTTP callback
    os.environ['DEBUG'] = "1"
    app.secret_key = os.urandom(24)
    app.run(debug=True)

@@ -1,12 +1,28 @@
+import requests%0A
 from request
@@ -513,81 +513,81 @@
 = %22k
-N6aHLmmI30UXtcwTCf1iAKaf5Ya%22%0Aclient_secret = %22imTCjzNXrRZVpbq0Xlx3LWr8eww
+B9W2k3wJEofjIlUdnjoWjQ_P_Qa%22%0Aclient_secret = %22OTSI4TQRBfSFfzbNdmEGaqg1eqU
 a%22%0As
@@ -598,18 +598,11 @@
  = %22
-sample_key
+all
 %22%0A%0Ah
@@ -2643,22 +2643,30 @@
 l_for('.
-profil
+consume_servic
 e'))%0A%0A%0A@
@@ -2677,22 +2677,30 @@
 route(%22/
-profil
+consume_servic
 e%22, meth
@@ -2716,22 +2716,30 @@
 %22%5D)%0Adef 
-profil
+consume_servic
 e():%0A   
@@ -2803,24 +2803,26 @@
 %0A    %22%22%22%0A   
+ #
  print(%22Done
@@ -2828,16 +2828,18 @@
 e!%22)%0A   
+ #
  print(%22
@@ -2905,60 +2905,394 @@
     
-return jsonify(%22%7B%7B'done': True, 'access_token'
+access_token = session%5B'oauth_token'%5D%5B'access_token'%5D%0A    endpoint = %22https://localhost:8243/t/knnect.com/ebill_new/1.0.0/ebill%22%0A    headers = %7B%0A        %22Authorization%22: %22%7Btype%7D %7Bvalue%7D%22.format(type=%22Bearer%22, value=access_token)%0A    %7D%0A    applications = requests.get(endpoint, headers=headers, verify=False)%0A    if not applications.ok:%0A        message = %22ERROR: Something went wrong
 : %7B%7D
-%7D%7D
 %22.fo
@@ -3296,38 +3296,152 @@
 .format(
-session%5B'oauth_token'%5D
+applications.content)%0A        print(message)%0A        return jsonify(%22'Error': %7B%7D%22.format(message))%0A    return jsonify(applications.json(
 ))%0A%0A%0Aif 

Make improvments to sample flask playground app