question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

401 Client Error: Unauthorized for url, example q01

See original GitHub issue

Attempting to run example q01 for paper trading and encountering error

(myvirtualenv) ➜  src bash ../credentials/config.sh; pylivetrader run -f algo.py -l 'DEBUG' 
[2018-12-18 17:17:17.226250] INFO: Algorithm: livetrader start running with backend = alpaca data-frequency = minute
0
[2018-12-18 17:17:19.468488] INFO: algo: 
        Algorithm initialized variables:
            context.MaxCandidates 100
            LowVar 6
            HighVar 40
        
[2018-12-18 17:17:19.472587] INFO: algo: 
        Algorithm selected stocks NumExprFilter(expr='x_0 <= (100.0)', bindings={'x_0': Rank(NumExprFactor(expr='(x_0 - x_1) / (x_1)', bindings={'x_1': SimpleMovingAverage([USEquityPricing.close], 45), 'x_0': SimpleMovingAverage([USEquityPricing.close], 3)}), method='ordinal', mask=AssetExists())})
        
/home/me/.zipline/data/dailycache/iex_company.pkl: digest mismatch 1ecedfa98cc42ea86945c4ddb7eaaffc != b82e657814f0790f98496814b11fb538, reloading
/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/iexfinance/__init__.py:45: UserWarning: Stock is moved to iexfinance.stocks. This function will in be deprecated in v0.4.0
  warnings.warn(WNG_MSG % ("Stock", "stocks"))
[2018-12-18 17:17:21.607837] DEBUG: pipeline_live.data.sources.util: fetch: 10.18% completed
[2018-12-18 17:17:22.294798] DEBUG: pipeline_live.data.sources.util: fetch: 20.35% completed
[2018-12-18 17:17:22.996512] DEBUG: pipeline_live.data.sources.util: fetch: 30.53% completed
[2018-12-18 17:17:23.692973] DEBUG: pipeline_live.data.sources.util: fetch: 40.70% completed
[2018-12-18 17:17:24.354522] DEBUG: pipeline_live.data.sources.util: fetch: 50.88% completed
[2018-12-18 17:17:25.018852] DEBUG: pipeline_live.data.sources.util: fetch: 61.06% completed
[2018-12-18 17:17:25.693408] DEBUG: pipeline_live.data.sources.util: fetch: 70.10% completed
[2018-12-18 17:17:26.351974] DEBUG: pipeline_live.data.sources.util: fetch: 80.28% completed
[2018-12-18 17:17:26.844290] DEBUG: pipeline_live.data.sources.util: fetch: 90.45% completed
[2018-12-18 17:17:27.475785] DEBUG: pipeline_live.data.sources.util: fetch: 100.00% completed
[2018-12-18 17:17:27.573776] INFO: pipeline_live.data.iex.pricing_loader: chart_range=3m
/home/me/.zipline/data/dailycache/iex_chart_3m: digest mismatch 1ecedfa98cc42ea86945c4ddb7eaaffc != b82e657814f0790f98496814b11fb538, reloading
[2018-12-18 17:17:36.361035] DEBUG: pipeline_live.data.sources.util: fetch: 10.18% completed
[2018-12-18 17:17:40.283283] DEBUG: pipeline_live.data.sources.util: fetch: 20.35% completed
[2018-12-18 17:17:43.810842] DEBUG: pipeline_live.data.sources.util: fetch: 30.53% completed
[2018-12-18 17:17:46.952893] DEBUG: pipeline_live.data.sources.util: fetch: 40.70% completed
[2018-12-18 17:17:49.735045] DEBUG: pipeline_live.data.sources.util: fetch: 50.88% completed
[2018-12-18 17:17:51.555943] DEBUG: pipeline_live.data.sources.util: fetch: 61.06% completed
[2018-12-18 17:17:52.823105] DEBUG: pipeline_live.data.sources.util: fetch: 70.10% completed
[2018-12-18 17:17:54.573683] DEBUG: pipeline_live.data.sources.util: fetch: 80.28% completed
[2018-12-18 17:17:56.343674] DEBUG: pipeline_live.data.sources.util: fetch: 90.45% completed
[2018-12-18 17:17:57.761533] DEBUG: pipeline_live.data.sources.util: fetch: 100.00% completed
[2018-12-18 17:18:01.062911] INFO: pipeline_live.data.iex.pricing_loader: chart_range=3m
/home/me/.zipline/data/dailycache/iex_key_stats.pkl: digest mismatch 1ecedfa98cc42ea86945c4ddb7eaaffc != b82e657814f0790f98496814b11fb538, reloading
[2018-12-18 17:18:09.170879] DEBUG: pipeline_live.data.sources.util: fetch: 10.18% completed
[2018-12-18 17:18:10.233156] DEBUG: pipeline_live.data.sources.util: fetch: 20.35% completed
[2018-12-18 17:18:10.678750] DEBUG: pipeline_live.data.sources.util: fetch: 30.53% completed
[2018-12-18 17:18:11.445735] DEBUG: pipeline_live.data.sources.util: fetch: 40.70% completed
[2018-12-18 17:18:12.105574] DEBUG: pipeline_live.data.sources.util: fetch: 50.88% completed
[2018-12-18 17:18:12.747900] DEBUG: pipeline_live.data.sources.util: fetch: 61.06% completed
[2018-12-18 17:18:13.421420] DEBUG: pipeline_live.data.sources.util: fetch: 70.10% completed
[2018-12-18 17:18:14.081844] DEBUG: pipeline_live.data.sources.util: fetch: 80.28% completed
[2018-12-18 17:18:14.631220] DEBUG: pipeline_live.data.sources.util: fetch: 90.45% completed
[2018-12-18 17:18:15.420115] DEBUG: pipeline_live.data.sources.util: fetch: 100.00% completed
Traceback (most recent call last):
  File "/home/me/python-virtualenvs/myvirtualenv/bin/pylivetrader", line 11, in <module>
    sys.exit(main())
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pylivetrader/__main__.py", line 144, in run
    algorithm.run(retry=ctx.retry)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pylivetrader/algorithm.py", line 242, in run
    return self.executor.run(retry=retry)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pylivetrader/executor/executor.py", line 120, in run
    algo.before_trading_start(self.current_data)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pylivetrader/algorithm.py", line 215, in before_trading_start
    self._before_trading_start(self, data)
  File "algo.py", line 204, in before_trading_start
    
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pylivetrader/misc/api_context.py", line 62, in wrapped
    return getattr(algorithm, f.__name__)(*args, **kwargs)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pylivetrader/algorithm.py", line 1049, in pipeline_output
    output = eng.run_pipeline(self._pipelines[name])
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/engine.py", line 77, in run_pipeline
    initial_workspace,
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/engine.py", line 252, in compute_chunk
    mask,
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/zipline/pipeline/mixins.py", line 214, in _compute
    compute(date, masked_assets, out_row, *inputs, **params)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/data/polygon/filters.py", line 12, in compute
    company = polygon.company()
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/data/sources/polygon.py", line 17, in company
    return _company(all_symbols)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/data/sources/util.py", line 68, in wrapper
    body = func(*args, **kwargs)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/data/sources/polygon.py", line 32, in _company
    return parallelize(fetch, workers=25, splitlen=50)(all_symbols)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/data/sources/util.py", line 30, in wrapper
    task_result = task.result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 398, in result
    return self.__get_result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/pipeline_live/data/sources/polygon.py", line 27, in fetch
    response = api.polygon.get('/meta/symbols/company', params=params)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/alpaca_trade_api/polygon/rest.py", line 33, in get
    return self._request('GET', path, params=params)
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/alpaca_trade_api/polygon/rest.py", line 29, in _request
    resp.raise_for_status()
  File "/home/me/python-virtualenvs/myvirtualenv/lib/python3.5/site-packages/requests/models.py", line 935, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
r    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.polygon.io/v1/meta/symbols/company?symbols=BMRA%2CBWZ%2CMSBF%2CECT%2CARKG%2CMIDU%2CESGU%2CMYOS%2CCTHR%2CJLS%2CNBO%2CSOIL%2CMAV%2CPPT%2CUIVM%2CFORK%2CNMM%2CCDXC%2CSYPR%2CNVFY%2CFEUZ%2CFMB%2CNVUS%2CPME%2CJHMI%2CIGN%2CWMCR%2CLOAC%2CTHD%2CKDMN%2CWWR%2CBTO%2CVIIX%2CPFI%2CDSE%2CTPYP%2CHYXU%2CSRF%2CFLJH%2CDVYA%2CCFA%2CDWT%2CFEMB%2CDTEA%2CCHAD%2CFAMI%2CEEB%2CNMS%2CDHCP%2CHNP&apiKey=AKTV6DOG63CG9DWK91G1

environment variables look like

(myvirtualenv) ➜  src env
...
APCA_API_KEY_ID=AKTV6DOG63CG9DWK91G1
APCA_API_SECRET_KEY=xxxxxx/xxxxxx/xxxxxx
APCA_API_BASE_URL=https://paper-api.alpaca.markets

Note, have note changed the code for example q01 and am simply trying to test it as is on paper trading when this error is being thrown.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ttt733commented, Dec 18, 2018

Only brokerage accounts have access to the premium Polygon data service. For now, we are providing this data to all brokerage accounts - i.e., those accounts that have the ability to live trade - free of charge, but in the future, that data is likely to move to only accounts that subscribe to that data service. The expectation is that most people will be using Alpaca’s data API. I’m working on fixing up our documented examples to make things like this clearer. I’ll keep this issue open for now as a reminder to clean up (i.e. change the API used in) q01, but in the meantime, you can either register your paper trading account to get live trading permissions or change the code yourself to use the Alpaca data API instead of the Polygon one.

0reactions
daraulcommented, May 4, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

401 Client Error: Unauthorized for url - python - Stack Overflow
In general 401 is your not authorized for the particular resource. · I see but I can't figure out why. · It means...
Read more >
Error 401 Client Error: Unauthorized for url - Hub
Hi @talids, you're getting a 401 error because you're not authenticated when making the request, and either: the model doesn't exist; the model ......
Read more >
How to Quickly Fix the 401 Unauthorized Error (5 Methods)
Look for Errors in the URL; Clear Your Browser's Cache; Flush Your DNS; Deactivate Your WordPress Plugins; Check the WWW-Authenticate Header ...
Read more >
401 Error: 5 Ways to Troubleshoot and Fix It - Hostinger
The 401 Unauthorized error is triggered by unauthenticated requests made to a WordPress web server. Learn how to identify and fix the issue....
Read more >
401 Unauthorized Error: What It Is and How to Fix It
The 401 Unauthorized Error is an HTTP response status code indicating that the client could not authenticate a request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found