简体   繁体   中英

Desire2Learn Valence API | JSON not loading

I'm using the Python Requests library with the Valence-provided Python SDK to attempt to do a GET request. Something odd is happening with the URL and I'm not sure what. The response I get is 200 (which leads me to believe that the authentication is working), but when I try to print the JSON from the Request object, it instead prints the HTML of the page instead of the JSON.

I'm using modified code that I read from http://docs.valence.desire2learn.com/clients/python/auth.html .

Here's the Python code:

import requests
import auth as d2lauth
from auth import *

app_creds = { 'app_id': '----', 'app_key': '----' }

ac = d2lauth.fashion_app_context(app_id=app_creds['app_id'], app_key=app_creds['app_key'])

auth_url = ac.create_url_for_authentication('ugatest2.view.usg.edu', 'http://localhost:8080')

redirect_url = "https://localhost:8080?x_a=3----&x_b=3dMRgCBAHXJDTA2E6DJIfdWq-gYl-pk77fF_3X5oDUuqc"

uc = ac.create_user_context(auth_url, 'ugatest2.view.usg.edu', True)

route = 'ugatest2.view.usg.edu/d2l/api/versions/'

url = uc.create_authenticated_url(route)

r = requests.get(url)

print(r.text) 

The output is the HTML of a page instead of JSON. If I do print(r), I get a status of 200. I think my redirect URL may be the issue, but I'm not sure what exactly is wrong. Thanks for any help!

Two things look off to me:

  • Using auth_url to create a user context isn't going to work, that's the URL you need to send the user to so they can authenticate. You need to use the URL you were redirected to after authenticating to build the user context. Assuming redirect_url is that URL, you should be passing that to create_user_context and not auth_url .

  • ugatest2.view.usg.edu/d2l/api/versions/ is not a valid value for passing to create_authenticated_route , /d2l/api/versions is probably what you want. The SDK will prepend the scheme, domain, and port so including those in the value passed is going to result in an incorrect URI.

一旦您的应用正常运行,您就可以使用r.json()而不是r.text来访问JSON响应。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM