simplejson.errors.JSONDecodeError when using client id and secret
Hello,
Here is the code I'm using
baseurl = 'https://somecompany.sharepoint.com'
basesite = '/sites/someteam' # every share point has a home.
siteurl = baseurl + basesite
localpath = './testfile.txt'
remotepath = 'Shared Documents/testfile.txt' # existing folder path under sharepoint site.
app_principal = {
'client_id': 'xxxxxxxxxxxxxxxxxxxxxxxxx',
'client_secret': 'xxxxxxxxxxxxxxxxxxxxxxxxx',
}
context_auth = AuthenticationContext(url=siteurl)
context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
ctx = ClientContext(siteurl, context_auth)
with open(localpath, 'rb') as content_file:
file_content = content_file.read()
dir, name = path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()
This code fails at the last line with this traceback
Traceback (most recent call last):
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\sharepoint_test.py", line 30, in <module>
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\runtime\client_object.py", line 33, in execute_query
self.context.execute_query()
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\runtime\client_runtime_context.py", line 138, in execute_query
self.pending_request().execute_query()
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\runtime\client_request.py", line 73, in execute_query
self.beforeExecute.notify(request)
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\runtime\types\EventHandler.py", line 18, in notify
listener(*args, **kwargs)
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\sharepoint\client_context.py", line 164, in _build_modification_query
self.ensure_form_digest(request)
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\sharepoint\client_context.py", line 125, in ensure_form_digest
self.request_form_digest()
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\office365\sharepoint\client_context.py", line 133, in request_form_digest
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\simplejson\__init__.py", line 525, in loads
return _default_decoder.decode(s)
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\simplejson\decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "C:\Users\tjo\Documents\Python_Scripts\sharepoint_test\venv\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I can upload fine when I'm using user/password authentication.
I am getting the same error. Did you ever figure out the problem?
Any updates on this issue? Me too got the same error
Same error, even though the authentication is working.
Greetings,
this might be the case (source):
For new tenants, apps using an ACS app-only access token is disabled by default.
How to determine whether App-Only auth is enabled:
admin_client = ClientContext(admin_site_url).with_credentials(test_user_credentials)
tenant = Tenant(admin_client).get().execute_query()
print(tenant.get_property("DisableCustomAppAuthentication"))
which is expected to return False
In case if disabled (True), the following solutions could be considered:
Option 1: enable App-Only auth
admin_credentials = UserCredential(username, password)
admin_client = ClientContext(admin_site_url).with_credentials(admin_credentials)
tenant = Tenant(admin_client).get().execute_query()
if tenant.get_property("DisableCustomAppAuthentication"):
tenant.set_property("DisableCustomAppAuthentication", False).update().execute_query()
Option 2: Utilize client certificate flow instead
cert_settings = {
'client_id': '--client id goes here--,
'thumbprint': "--thumbprint goes here--",
'cert_path': './selfsigncert.pem',
}
ctx = ClientContext(site_url).with_client_certificate(test_tenant, **cert_settings)
current_web = ctx.web.get().execute_query()
Refer wiki for a more details
/cc @dhgokul, @tomerharduf-vayyar
Hi @vgrem, working in my side. Ty for the quick response. :)
Hi guys,
So we have registered an app and created with cert, its been given access on all sites however I can upload a file to site below it?