json.decoder.JSONDecodeError: Extra data: line 1 column 427 (char 426) - python

I`m trying to upload a video on youtube via a script, but when I execute the script I get this error. The json file is working as I tested it with another script that does the same thing in a different way.I tried everything this is my last chance or I will give up on this script.
If you need more details please let me know, I appreciate your effort!
This is the error I get:
Extra data: line 1 column 427 (char 426)
Traceback (most recent call last):
File "publisher/botTest.py", line 54, in <module>
youtubeService.uploadVideoToYouTube(config)
File "/root/publisher/core/services/youtube.py", line 202, in uploadVideoToYouTube
youtube = getAuthenticatedService(CREDENTIALS_FILE)
File "/root/publisher/core/services/youtube.py", line 69, in getAuthenticatedService
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES,redirect_uri='urn:ietf:wg:oauth:2.0:oob')
File "/usr/local/lib/python3.6/dist-packages/google_auth_oauthlib/flow.py",
line 172, in from_client_secrets_file
client_config = json.load(json_file)
File "/usr/lib/python3.6/json/__init__.py", line 299, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 342, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 427 (char 426)
line 202 form youtube.py:
def uploadVideoToYouTube(config):
CREDENTIALS_FILE = constants.SECRETS_ROOT_LOCATION + config['channel'][1] + '/' +
constants.YOUTUBE_CHANNEL_CREDENTIALS_FILE_NAME
if os.path.isfile(CREDENTIALS_FILE):
youtube = getAuthenticatedServiceFromStorage(CREDENTIALS_FILE)
print(os.path.isfile(CREDENTIALS_FILE))
print(CREDENTIALS_FILE) else:
os.makedirs(constants.SECRETS_ROOT_LOCATION + config['channel'][1])
youtube = getAuthenticatedService(CREDENTIALS_FILE)
line 69 in youtube.py:
# Authorize the request and store authorization credentials. Used to
# generate first auth token. Only needs to happen once.
def getAuthenticatedService(CREDENTIALS_FILE):
flow =InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES,
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_url, _ = flow.authorization_url(prompt='consent')
print('Please go to this URL: {}'.format(auth_url))
code = input('Enter the authorization code: ')
credentials = flow.fetch_token(code=code)
saveCredentials(CREDENTIALS_FILE, credentials)
return build(API_SERVICE_NAME, API_VERSION, credentials = flow.credentials)

Related

How to fix: azure.core.exceptions.ServiceRequestError: EOF occurred in violation of protocol (_ssl.c:1129)

I am trying to interact with the TextAnalyticsClient of Azure to run a pretrained custom Entity Recognition program. However, when I call the attribute .begin_recognize_custom_entities(...) in the code shown below. It appears that the administration of the key credentials passes succesfully (in text_analytics_client = TextAnalyticsClient(...)), but then it fails afterwards.
"""
FILE: sample_recognize_custom_entities.py
DESCRIPTION:
This sample demonstrates how to recognize custom entities in documents.
Recognizing custom entities is available as an action type through the begin_analyze_actions API.
For information on regional support of custom features and how to train a model to
recognize custom entities, see https://aka.ms/azsdk/textanalytics/customentityrecognition
USAGE:
python sample_recognize_custom_entities.py
Set the environment variables with your own values before running the sample:
1) AZURE_LANGUAGE_ENDPOINT - the endpoint to your Language resource.
2) AZURE_LANGUAGE_KEY - your Language subscription key
3) CUSTOM_ENTITIES_PROJECT_NAME - your Language Studio project name
4) CUSTOM_ENTITIES_DEPLOYMENT_NAME - your Language Studio deployment name
"""
import os
print('#############################################################')
def sample_recognize_custom_entities():
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
deployment_name = #####
endpoint = #####
key = #####
project_name = #####
path_to_sample_document = r'C:\Users\#####\Documents\#####\Source\Document cracking\output_documents\txt_for_ML_566docs_270922\text.txt'
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=AzureKeyCredential(key),
)
with open(path_to_sample_document) as fd:
document = [fd.read()]
poller = text_analytics_client.begin_recognize_custom_entities(
document,
project_name=project_name,
deployment_name=deployment_name
)
document_results = poller.result()
for result in document_results:
custom_entities_result = result[0] # first document, first result
if not custom_entities_result.is_error:
for entity in custom_entities_result.entities:
print(
"Entity '{}' has category '{}' with confidence score of '{}'".format(
entity.text, entity.category, entity.confidence_score
)
)
else:
print(
"...Is an error with code '{}' and message '{}'".format(
custom_entities_result.code, custom_entities_result.message
)
)
sample_recognize_custom_entities()
if __name__ == "__main__":
sample_recognize_custom_entities()
it gives the following error:
File "C:\Users\#####\Documents\#####\Source\Document cracking\crack-document\test_ML_connection.py", line 71, in <module>
sample_recognize_custom_entities()
File "C:\Users\#####\Documents\#####\Source\Document cracking\crack-document\test_ML_connection.py", line 48, in sample_recognize_custom_entities
poller = text_analytics_client.begin_recognize_custom_entities(
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_validate.py", line 74, in wrapper
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_text_analytics_client.py", line 1388, in begin_recognize_custom_entities
self.begin_analyze_actions(
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_validate.py", line 74, in wrapper
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_text_analytics_client.py", line 1213, in begin_analyze_actions
self._client.begin_analyze_text_submit_job(
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_generated\_operations_mixin.py", line 277, in begin_analyze_text_submit_job
return mixin_instance.begin_analyze_text_submit_job(body, **kwargs)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
return func(*args, **kwargs)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_generated\v2022_05_01\operations\_patch.py", line 67, in begin_analyze_text_submit_job
raw_result = self._analyze_text_submit_job_initial( # type: ignore
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\ai\textanalytics\_generated\v2022_05_01\operations\_text_analytics_client_operations.py", line 353, in _analyze_text_submit_job_initial
pipeline_response = self._client._pipeline.run( # type: ignore # pylint: disable=protected-access
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 211, in run
return first_node.send(pipeline_request) # type: ignore
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
[Previous line repeated 2 more times]
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\policies\_redirect.py", line 158, in send
response = self.next.send(request)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\policies\_retry.py", line 468, in send
raise err
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\policies\_retry.py", line 446, in send
response = self.next.send(request)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 71, in send
response = self.next.send(request)
[Previous line repeated 3 more times]
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\_base.py", line 103, in send
self._sender.send(request.http_request, **request.context.options),
File "C:\Users\#####\Anaconda3\envs\doc_crac\lib\site-packages\azure\core\pipeline\transport\_requests_basic.py", line 361, in send
raise error
azure.core.exceptions.ServiceRequestError: EOF occurred in violation of protocol (_ssl.c:1129)
Anyone knows what this means, and how to solve it?
Thanks in advance,
Koeiswit

Azure TableService() returns python ValueError in prod, but not dev

I've got dev Python code running GREAT that uses...
1 from azure.cosmosdb.table.tableservice import TableService
2 from azure.cosmosdb.table.models import Entity
...
85 table_service = TableService(connection_string = table_storage_connection_string)
86 perform_lookup = table_service.get_entity(mapping_table, 'test', 'foo')
After copying the code to prod and changing the values of...
table_storage_connection_string
Docs say using connection_string is ok.
Validated this is a connection string to the Table Storage Account
It IS using a Key Vault reference in local.settings.json/Azure Portal App Settings
mapping_table
...I get the error:
Exception while executing function: Functions.my-func-prod <--- Result: Failure Exception: ValueError: You need to provide an account name and either an account_key or sas_token when creating a storage service. Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 343, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 480, in __run_sync_func return func(**params) File "/home/site/wwwroot/my-func-prod/__init__.py", line 85, in main table_service = TableService(connection_string = table_storage_connection_string) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/tableservice.py", line 173, in __init__ service_params = _TableServiceParameters.get_service_parameters( File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_connection.py", line 110, in get_service_parameters params = _ServiceParameters._from_connection_string(connection_string, service) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_connection.py", line 153, in _from_connection_string return _ServiceParameters(service, File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_connection.py", line 85, in __init__ raise ValueError(_ERROR_STORAGE_MISSING_INFO)
prop__StartTime 2020-10-16T04:50:07.099Z
prop__Duration 00:00:01.2898150
ProcessId 18
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.my-func-prod
Why would TableService() work just fine in dev with a connection string, require the Table Storage account name and account key or sas token when in prod?
EDIT 1:
When connection_string is substituted with account_name and account_key of the storage account, I get a new error on the subsequent line (89):
1 from azure.cosmosdb.table.tableservice import TableService
2 from azure.cosmosdb.table.models import Entity
...
85 table_service = TableService(
86 account_name = table_storage_account_name,
87 account_key = table_storage_account_key
88 )
89 perform_lookup = table_service.get_entity(mapping_table, 'test', 'foo')
New Error:
Exception while executing function: Functions.my-func-prod <--- Result: Failure Exception: AzureSigningError: Incorrect padding Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 343, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 480, in __run_sync_func return func(**params) File "/home/site/wwwroot/my-func-prod/__init__.py", line 89, in main perform_lookup = table_service.get_entity(mapping_table, 'test', 'foo') File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/tableservice.py", line 896, in get_entity return self._perform_request(request, _convert_json_response_to_entity, File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/tableservice.py", line 1106, in _perform_request return super(TableService, self)._perform_request(request, parser, parser_args, operation_context) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/storageclient.py", line 386, in _perform_request raise ex File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/storageclient.py", line 358, in _perform_request raise ex File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/storageclient.py", line 302, in _perform_request self.authentication.sign_request(request) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/_auth.py", line 35, in sign_request self._add_authorization_header(request, string_to_sign) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/cosmosdb/table/common/_auth.py", line 75, in _add_authorization_header raise _wrap_exception(ex, AzureSigningError)
Anyone overcome this issue before with Table Storage?
I tried to reproduce your error, but both using connection_string and using account_name and account_key all worked well.
The connection_string contains both AccountName and AccountKey, looks like this:
DefaultEndpointsProtocol=https;AccountName=<your-account-name>;AccountKey=<e60xxxxxxxxxxx==>;EndpointSuffix=core.windows.net
If the connection_string does not include the AccountName or AccountKey, the error will appear.
The error(Incorrect padding) is related to your account key being incorrect. Please regenerate the key in the portal and try with the new one.

Saving and generating user generated data with python and json

I am using Python Crash Course book by Eric Matthes and have a problem with my code from examples page 211. I am attempting to save multiple usenames in a json doc and then retrieve them if username is stored, but if its a new username I would like it to store and not retrieve, so that multiple usernames are stored.
I am receiving JSONDecodeError: Expecting value
import json
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
username = input("Username: ")
with open(filename, 'w') as f_obj:
json.dump(username, f_obj)
print("Username stored. Thanks " + username + "!")
else:
print("Welcome back " + username +"!" + " You receive a returning
customer discount.")
Traceback:
runfile('/home/jubal/ CrashCourse Python Notes/Chapter 10 CC/returning_user.py', wdir='/home/jubal/ CrashCourse Python Notes/Chapter 10 CC')
Traceback (most recent call last):
File "/home/jubal/ CrashCourse Python Notes/Chapter 10 CC/returning_user.py", line 7, in <module>
username = json.load(f_obj)
File "/home/jubal/anaconda3/lib/python3.7/json/__init__.py", line 296, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/home/jubal/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/home/jubal/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/jubal/anaconda3/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
JSONDecodeError: Expecting value
Any help would be appreciated. Thanks for your time!

JSON writing to a file error, Extra data: line 1 column 2347 (char 2346)

import json
import requests
response = requests.get('SOME_LINK_THAT_IVE_REMOVED')
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
pass
#print (response.text)
with open('stats.json', 'w') as output:
json.dump(response.json(), output, indent = 4)
what it printed:
{"sessionid":"F9269D5D-2B5C-432B-B8BC-34F87F790619","game_clock_display":"02:12.84","game_clock":132.8432,"game_status":"playing","possession":[0,2],"teams":[{"players":[{"name":"Nooth","playerid":2,"position":[-8.6900005,-0.28800002,-8.6470003],"stats":{"possession_time":22.589041,"points":0,"saves":0,"goals":0,"stuns":3,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":0},"userid":658915067565875,"possession":false},{"name":"erikmelkumyan","playerid":3,"position":[-0.72400004,1.7060001,-28.595001],"stats":{"possession_time":9.5638027,"points":0,"saves":0,"goals":0,"stuns":3,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":2126518170756015,"possession":false},{"name":"Sandman187_","playerid":4,"position":[-2.3990002,2.3380001,-26.783001],"stats":{"possession_time":27.565685,"points":0,"saves":1,"goals":0,"stuns":4,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":1611289978936588,"possession":true}],"team":"BLUE
TEAM","possession":true,"stats":{"points":0,"possession_time":59.718529,"interceptions":0,"blocks":0,"steals":0,"catches":0,"passes":0,"saves":1,"goals":0,"stuns":10,"assists":0,"shots_taken":2}},{"players":[{"name":"MooneyWhy","playerid":0,"position":[-4.539,1.399,-13.481001],"stats":{"possession_time":14.364853,"points":2,"saves":1,"goals":0,"stuns":7,"passes":0,"catches":0,"steals":1,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":1265147863612788,"possession":false},{"name":"b-love","playerid":1,"position":[-11.484,2.072,0.70500004],"stats":{"possession_time":50.680099,"points":6,"saves":1,"goals":0,"stuns":1,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":1},"userid":1457786340976218,"possession":false},{"name":"onikaze","playerid":5,"position":[-7.6980004,1.268,-11.036],"stats":{"possession_time":17.629295,"points":0,"saves":0,"goals":0,"stuns":6,"passes":0,"catches":0,"steals":0,"blocks":0,"interceptions":0,"assists":0,"shots_taken":2},"userid":1636331273057819,"possession":false}],"team":"ORANGE
TEAM","possession":false,"stats":{"points":8,"possession_time":82.674248,"interceptions":0,"blocks":0,"steals":1,"catches":0,"passes":0,"saves":2,"goals":0,"stuns":14,"assists":0,"shots_taken":4}}]}
Traceback (most recent call last):
File "C:\Users\Kai\Desktop\Python\testv4.py", line 19, in <module>
json.dump(response.json(), output, indent = 4)
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py",
line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\json__init__.py",
line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\Kai\AppData\Local\Programs\Python\Python37-32\lib\json\decoder.py",
line 340, in decode
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 2347 (char 2346)
Any help? Seems to create invalid JSON file in the text file.
Creates a NULL value on the end of the JSON files when viewed in NotePad++

method put python unauthorized 401

i have a python script , i will send an image to server, so i use "put" method ,
the server respond : 401 unauthorized
======================================================================
FAIL: test_EnvoiPhotos (__main__.EnvoiPhotos)
----------------------------------------------------------------------
Traceback (most recent call last):
File "build/bdist.linux-i686/egg/funkload/FunkLoadTestCase.py", line 946, in __call__
testMethod()
File "test_EnvoiPhotos.py", line 74, in test_EnvoiPhotos
self.put(self.server_url + "/contacts/41.json",Data('application/json',data),description = "put photo")
File "build/bdist.linux-i686/egg/funkload/FunkLoadTestCase.py", line 408, in put
load_auto_links=load_auto_links)
File "build/bdist.linux-i686/egg/funkload/FunkLoadTestCase.py", line 401, in method
method=method, load_auto_links=load_auto_links)
File "build/bdist.linux-i686/egg/funkload/FunkLoadTestCase.py", line 299, in _browse
response = self._connect(url, params, ok_codes, method, description)
File "build/bdist.linux-i686/egg/funkload/FunkLoadTestCase.py", line 216, in _connect
raise self.failureException, str(value.response)
AssertionError: /contacts/41.json
HTTP Response 401: Unauthorized
i use funkload to test my application web
this is a part of my code :
#generation nombre between 1 et 109
i = random.randint(1,109)
#read photo
nameimage = "im"
nameimage=nameimage+str(i).zfill(1)+'.jpg'
self.logd(nameimage)
filephoto=open(nameimage,'rb')
image_data=filephoto.read()
filephoto.close()
b64_text = base64.b64encode(image_data)
const_image = {}
const_image['id'] = i
const_image['first_name'] = 'guest'
const_image['last_name'] = 'guest'
const_image['playload'] = b64_text
data=json.dumps(const_image)
#self.logd(data)
self.setHeader('content-type', 'application/json')
#next request
self.put(self.server_url+"/contacts/41.json",Data('application/json',data),description = "put photo")

Categories