In my use-case I am building a MS Graph file browser which receives a @odata.nextLink value from Graph API calls, which look like https://graph.microsoft.com/v1.0/groups/myGroupId/drive/items('driveItem')/children?$select=id,name,webUrl&$top=10&$skiptoken=someToken.
In my main python script I add a quote function to the jinja environment using urllib.parse.quote. We need this to encode the URLs within our jinja templates:
import urllib
app.jinja_env.globals.update(Auth=identity.web.Auth, quote=urllib.parse.quote)
In my Jinja template I use the following to encode the result['@odata.nextLink'] URL and make sure to set safe='' so the / characters are also encoded. This encoded URL is sent to our odNextLink route:
<a href="{{ url_for('odNextLink', nextLink=quote(result['@odata.nextLink'], safe='')) }}">Next link</a>
In our route we need to make sure we have import urllib so we can unencoded the URL using urllib.parse.unquote(nextLink) before calling the API endpoint.
# this route should return results from a Graph API 'nextLink' call
@app.route("/od_group_items/<nextLink>")
def odNextLink(nextLink):
import urllib
token = app.auth.get_token_for_user(ast.literal_eval(os.getenv('SCOPE')))
if "error" in token:
return redirect(url_for("login"))
api_result = requests.get(
urllib.parse.unquote(nextLink),
headers={'Authorization': 'Bearer ' + token['access_token']},
timeout=30,
).json()
return api_result