Office365-REST-Python-Client
Office365-REST-Python-Client copied to clipboard
Issue connecting my script to GCC High SharePoint API
Hello!
I'm trying to write a script to pull all of the list items from a SharePoint list and I cannot for the life of me get it to authenticate properly. Thank you for your help! I really don't know what to do. My script is below:
import json
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.listitems.collection import ListItemCollection
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.auth.client_credential import ClientCredential
config = json.load(open(file='parameters.json', encoding="utf-8"))
client_id = config["client_id"]
client_secret = config["client_secret"]
tenant = config["tenant"]
user_principal_name = config["user_principal_name"]
def print_progress(items):
# type: (ListItemCollection) -> None
print("Items read: {0}".format(len(items)))
# SharePoint site URL
site_url = config["site_url"]
# SharePoint list name
list_name = "Issue tracker"
# client_credentials = ClientCredential(client_id,client_secret)
# ctx = ClientContext(site_url).with_credentials(client_credentials, environment='GCCH')
ctx = ClientContext(site_url).with_interactive(tenant, client_id)
me = ctx.web.current_user.get().execute_query()
print(me)
web = ctx.web.get().execute_query()
print(web)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Web title: {0}".format(web.properties['Title']))
request = RequestOptions("{0}/_api/web/lists(guid'B945C1F3-B199-4412-B608-E38A990A297C')".format(site_url))
# request = RequestOptions("{0}/_api/web/lists".format(site_url))
response = ctx.pending_request().execute_request_direct(request)
data = json.loads(response.content)
data = json.dumps(data, indent=4, sort_keys=True)
print(data)
large_list = ctx.web.lists.get_by_title(list_name)
paged_items = (
large_list.items.paged(1000, page_loaded=print_progress).get().execute_query()
)
for index, item in enumerate(paged_items): # type: int, ListItem
pass
# print("{0}: {1}".format(index, item.id))
This is the output that I get:
Thomas Marconi
IT Department
Web title: IT Department
{
"error": {
"code": "-2147024891, System.UnauthorizedAccessException",
"message": {
"lang": "en-US",
"value": "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
}
}
}
Traceback (most recent call last):
File "/home/tmarconi/.local/lib/python3.10/site-packages/office365/runtime/client_request.py", line 38, in execute_query
response.raise_for_status()
File "/home/tmarconi/.local/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://tacticalairus.sharepoint.us/sites/ITDepartment2/_api/Web/lists/GetByTitle('Issue%20tracker')/items?$top=1000
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/mnt/c/Users/ThomasMarconi/Documents/programs/issue_dashboard/dashboard/update_database.py", line 48, in <module>
large_list.items.paged(1000, page_loaded=print_progress).get().execute_query()
File "/home/tmarconi/.local/lib/python3.10/site-packages/office365/runtime/client_object.py", line 53, in execute_query
self.context.execute_query()
File "/home/tmarconi/.local/lib/python3.10/site-packages/office365/runtime/client_runtime_context.py", line 173, in execute_query
self.pending_request().execute_query(qry)
File "/home/tmarconi/.local/lib/python3.10/site-packages/office365/runtime/client_request.py", line 42, in execute_query
raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: ('-1, System.ArgumentException', "List 'Issue tracker' does not exist at site with URL 'https://tacticalairus.sharepoint.us/sites/ITDepartment2'.", "404 Client Error: Not Found for url: https://tacticalairus.sharepoint.us/sites/ITDepartment2/_api/Web/lists/GetByTitle('Issue%20tracker')/items?$top=1000")
I had the same issue when trying to connect to outlook in GCC high, but I had to add
def construct_request(request): request.url = request.url.replace("https://graph.microsoft.com", "https://graph.microsoft.us")
this might fix your problem