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")
Related
I am trying to use Reddit's developer API to build a simple scraper that grabs posts and their replies in a target subreddit and produces JSON with the information.
I am getting a 404 error that I don't understand.
This is my code:
import praw
import json
def scrape(subreddit, limit):
r = praw.Reddit(user_agent='Reddit data organizer 1.0 by /u/reallymemorable', client_id='none of your business', client_secret='none of your business')
submissions = r.subreddit(subreddit).get_hot(limit=limit)
for submission in submissions:
data = {}
data['title'] = submission.title
data['score'] = submission.score
data['url'] = submission.url
data['author'] = str(submission.author)
data['subreddit'] = str(submission.subreddit)
data['num_comments'] = submission.num_comments
data['over_18'] = submission.over_18
data['selftext'] = submission.selftext
data['is_self'] = submission.is_self
data['name'] = submission.name
data['created_utc'] = submission.created_utc
data['permalink'] = submission.permalink
data['domain'] = submission.domain
data['id'] = submission.id
data['kind'] = submission.kind
json.dumps(data)
scrape('https://www.reddit.com/r/funny/', 25)
When I run it, I get this:
reallymemorable#Christians-MBP Desktop % python3 fetch-data-subreddit.py
Traceback (most recent call last):
File "/Users/reallymemorable/Desktop/fetch-data-subreddit.py", line 26, in <module>
scrape('https://www.reddit.com/r/augmentedreality/comments/yv7sn8/ar_maximum_distance/', 25)
File "/Users/reallymemorable/Desktop/fetch-data-subreddit.py", line 6, in scrape
submissions = r.subreddit(subreddit).get_hot(limit=limit)
File "/opt/homebrew/lib/python3.9/site-packages/praw/models/reddit/base.py", line 34, in __getattr__
self._fetch()
File "/opt/homebrew/lib/python3.9/site-packages/praw/models/reddit/subreddit.py", line 583, in _fetch
data = self._fetch_data()
File "/opt/homebrew/lib/python3.9/site-packages/praw/models/reddit/subreddit.py", line 580, in _fetch_data
return self._reddit.request(method="GET", params=params, path=path)
File "/opt/homebrew/lib/python3.9/site-packages/praw/util/deprecate_args.py", line 43, in wrapped
return func(**dict(zip(_old_args, args)), **kwargs)
File "/opt/homebrew/lib/python3.9/site-packages/praw/reddit.py", line 941, in request
return self._core.request(
File "/opt/homebrew/lib/python3.9/site-packages/prawcore/sessions.py", line 330, in request
return self._request_with_retries(
File "/opt/homebrew/lib/python3.9/site-packages/prawcore/sessions.py", line 266, in _request_with_retries
raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.NotFound: received 404 HTTP response
r.subreddit(subreddit) - subreddit should just be the name of the subreddit e.g. "funny" and not the full URL.
See the docs here: https://praw.readthedocs.io/en/stable/getting_started/quick_start.html#obtain-a-subreddit
This is the logs I received from my unittest for my APIs flask application which is using flask-restful, it showed that I have an int object not iterable error.
Really appreciate if anyone tell me what is actually wrong in my unittest codes :(
.
======================================================================
ERROR: test_brand_create (test.brand.BrandTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/david/ITP-Team3/test/brand.py", line 37, in test_brand_create
response = tester.post('/api/brand/', data=json.dumps(payload), headers=headers)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1140, in post
return self.open(*args, **kw)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/flask/testing.py", line 217, in open
return super().open(
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1089, in open
response = self.run_wsgi_app(request.environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 956, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1255, in run_wsgi_app
for item in app_iter:
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wsgi.py", line 462, in __next__
return self._next()
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 50, in _iter_encoded
for item in iterable:
TypeError: 'int' object is not iterable
======================================================================
ERROR: test_brand_put (test.brand.BrandTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/david/ITP-Team3/test/brand.py", line 49, in test_brand_put
response = tester.put('/api/brand/'+id, data=json.dumps(payload), headers=headers)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1145, in put
return self.open(*args, **kw)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/flask/testing.py", line 217, in open
return super().open(
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1089, in open
response = self.run_wsgi_app(request.environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 956, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/test.py", line 1255, in run_wsgi_app
for item in app_iter:
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wsgi.py", line 462, in __next__
return self._next()
File "/Users/david/.local/share/virtualenvs/ITP-Team3-bpvJtb_C/lib/python3.10/site-packages/werkzeug/wrappers/response.py", line 50, in _iter_encoded
for item in iterable:
TypeError: 'int' object is not iterable
----------------------------------------------------------------------
Ran 11 tests in 2.370s
FAILED (errors=2)
Here's my test code that I had the error on:
# brand.py
def test_brand_create(self):
tester = app.test_client(self)
headers = login(tester)
payload = {'brandName': 'test1'}
response = tester.post('/api/brand/', data=json.dumps(payload), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content_type, 'application/json')
def test_brand_put(self):
tester = app.test_client(self)
headers = login(tester)
payload = {'brandName': 'test'}
print("TYPE", type(get_id(app, Brand, 'test1')))
print("VALUE", get_id(app, Brand, 'test1'))
id = get_id(app, Brand, 'test1')
response = tester.put('/api/brand/'+id, data=json.dumps(payload), headers=headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content_type, 'application/json')
and the utils code as well
# utils.py
def login(tester):
headers = {'Content-Type': 'application/json'}
# put user and password in .env file
payload = {'username': os.getenv("user"), 'password_hash': os.getenv("password")}
response = tester.post('/api/user/login/', data= json.dumps(payload), headers=headers)
jwt_str = response.data.decode('utf-8')
header_jwt= ast.literal_eval(jwt_str)
return {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + header_jwt['jwt_token']}
def get_id(app, model, brandName) -> None:
with app.app_context():
test_query = model.query.filter_by(brandName=brandName).first()
id_lookup = model.query.get(test_query.id)
return str(id_lookup.id)
I am looking for some advice on how I can upsert or replace existing user entity.
I tried couple of API's documented here and also here.
The entities are read from database and the plan is to keep them in sync with database values as a scheduled job.
Update: Code Snippet
client_options = {"quota_project_id": gcp_default_project_id,
"api_endpoint": "us-central1-dialogflow.googleapis.com:443"}
client = EntityTypesClient(credentials=credentials_det, client_options=client_options)
entity_type = v3beta.EntityType()
entity_type.display_name = entity_display_name
entity_type.kind = "KIND_REGEXP"
print(client_options)
entity_type.entities = entity_json
# Initialize request argument(s)
request = v3beta.UpdateEntityTypeRequest(
entity_type=entity_type,
)
print(request)
response = client.update_entity_type(request=request)
print(response)
entity_json is fetched from DB and created as JSON object as below.
df = get_data.get_df_details(config_dir, entity_data_source, sql)
username = df['username'].tolist()
entity_json = []
for each in username:
each_entity_value = {}
each_entity_value['value'] = each
each_entity_value['synonyms'] = [each]
entity_json.append(each_entity_value)
Here's the Trace
Traceback (most recent call last):
File "/Users/<some_dir>/df_ins_entities/df_ins_entities/ins_entity_val.py", line 116, in
ins_now(config_dir, input_entity_name, entity_data_source)
File "/Users/<some_dir>/df_ins_entities/df_ins_entities/ins_entity_val.py", line 96, in ins_now
response = client.update_entity_type(request=request)
File "/Users/<some_dir>/df_ins_entities/lib/python3.9/site-packages/google/cloud/dialogflowcx_v3beta1/services/entity_types/client.py", line 902, in update_entity_type
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
File "/Users/<some_dir>/df_ins_entities/lib/python3.9/site-packages/google/api_core/gapic_v1/method.py", line 154, in call
return wrapped_func(args, **kwargs)
File "/Users/<some_dir>/df_ins_entities/lib/python3.9/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 Resource name '' does not match 'projects//locations//agents//entityTypes/*'.
Process finished with exit code 1
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)
I'm doing a POST request to an API, the code looks like this:
#gen.coroutine
def call():
...
response = yield AsyncHTTPClient().fetch(
HTTPRequest(
url='https://api.mywebsite.com/v1/users',
headers=headers,
method='POST',
body=json.dumps(body),
validate_cert=False
)
)
print response, response.body
if __name__ == "__main__":
tornado.ioloop.IOLoop.current().run_sync(call)
The server responds first time with 201 Created and the second time with 200 OK.
But for that code I get this error for the first time. The second time works
Traceback (most recent call last):
File "t.py", line 49, in <module>
tornado.ioloop.IOLoop.current().run_sync(call)
File "/usr/lib/python2.7/dist-packages/tornado/ioloop.py", line 389, in run_sync
return future_cell[0].result()
File "/usr/lib/python2.7/dist-packages/tornado/concurrent.py", line 129, in result
raise_exc_info(self.__exc_info)
File "/usr/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped
ret = fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 574, in inner
self.set_result(key, result)
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 500, in set_result
self.run()
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 529, in run
yielded = self.gen.throw(*exc_info)
File "t.py", line 43, in call
validate_cert=False
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 520, in run
next = self.yield_point.get_result()
File "/usr/lib/python2.7/dist-packages/tornado/gen.py", line 409, in get_result
return self.runner.pop_result(self.key).result()
File "/usr/lib/python2.7/dist-packages/tornado/concurrent.py", line 131, in result
return super(TracebackFuture, self).result(timeout=timeout)
File "/usr/lib/python2.7/dist-packages/concurrent/futures/_base.py", line 401, in result
return self.__get_result()
File "/usr/lib/python2.7/dist-packages/concurrent/futures/_base.py", line 360, in __get_result
raise self._exception
AssertionError
Looks like you are using sync function HTTPRequest in future
from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPResponse
def call():
response = None
http_client = AsyncHTTPClient()
try:
body = json.dumps(body)
response: HTTPResponse = yield http_client.fetch('https://api.mywebsite.com/v1/users', method='POST', body=str(body), headers=headers, request_timeout=5)
except Exception as e:
print('get_request error:{0}'.format(e))