> For the complete documentation index, see [llms.txt](https://gwen-leigh.gitbook.io/free-aws-cloud-project-bookcamp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gwen-leigh.gitbook.io/free-aws-cloud-project-bookcamp/week-2-distributed-tracing/2.3.0-rollbar/bug-fix-pyrollbar-no-access_token-provided..md).

# Bug Fix: pyrollbar: No access\_token provided.

Credentials and authentication issues preventing the rollbar agent in our backend from calling home.

## 1. Problem

* Although the rollbar `access_token` is configured with the python variable `rollbar_access_token`, the agent isn't calling home, and no data are coming through on the Rollbar side.

<div data-full-width="true"><figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FkHqomXUZJDNrxv2JZJyG%2Fimage.png?alt=media&amp;token=2007560f-2870-41c6-8972-1bf779aa8900" alt=""><figcaption></figcaption></figure></div>

## 2. Analysis

* Something must be wrong with the variable `rollbar_access_token` in line 94 (in the above image).&#x20;
* After good dozens of minutes, I finally figured out: there are multiple `access_tokens` at varying levels in Rollbar - one at the project level (for each project) and the other at the account level.&#x20;
* I was using the account level token where I have to input the project access token.

## 3. Solution

* [x] Grab the access token from the project.&#x20;
  * [x] `export` and `gp env` ROLLBAR\_ACCESS\_TOKEN

```
export ROLLBAR_ACCESS_TOKEN="YOUR_ROLLBAR_TOKEN_FROM_PROJECT"
gp env ROLLBAR_ACCESS_TOKEN="YOUR_ROLLBAR_TOKEN_FROM_PROJECT"
```

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2F8E0D1Qs6tVguMyIrrMSx%2Fimage.png?alt=media&amp;token=a4c828cc-6eb6-4b8d-b06d-0271cb7fdcd6" alt=""><figcaption><p>Unbelievable that Rollbar still hasn't updated this code that breaks. @app.before_first_request decorator was depricated from Flask 2.3 Also, there is no function call for init_rollbar(). Rollbar, really?</p></figcaption></figure>

However, I still get the same error that the `access_token` is not provided. Well... the error message was quite stragithforward in fact. It's been telling me to "call" the `rollbar.init()` function.

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FrGJWLH4vJPymHjbPkxRZ%2Fimage.png?alt=media&amp;token=15f93783-17b8-43c6-97cf-1d1d41c6fb35" alt=""><figcaption></figcaption></figure>

* [x] Reorganise the Rollbar code.
* [x] Call `init_rollbar()` under `with app.app_context():` function ([code fix here](https://github.com/mariachiinajar/aws-bootcamp-cruddur-2023-again/blob/02-03-rollbar/backend-flask/app.py)).

{% code title="app.py" lineNumbers="true" %}

```python
# ROLLBAR -------------------------
rollbar_access_token = os.getenv('ROLLBAR_ACCESS_TOKEN')

def init_rollbar():
    """init rollbar module"""
    rollbar.init(
        # access token
        rollbar_access_token,
        # environment name
        'production',
        # server root directory, makes tracebacks prettier
        root=os.path.dirname(os.path.realpath(__file__)),
        # flask already sets up logging
        allow_logging_basic_config=False)

    # send exceptions from `app` to rollbar, using flask's signal system.
    got_request_exception.connect(rollbar.contrib.flask.report_exception, app)


# @app.before_first_request # this decorator will break the code as it is depricated after Flask 2.2 
with app.app_context():     # use this 'with' statement instead. 
  init_rollbar()

# ROLLBAR TEST---------------------
@app.route('/rollbar/test')
def rollbar_test():
    rollbar.report_message('Hello Rollbar!', 'warning')
    return "Hello, Rollbar!"

```

{% endcode %}

## 4. Result

* The error message `pyrollbar: No access_token provided` is gone.&#x20;
* I check the Rollbar console and the spans are coming through.

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2F3HZZf7bNCKMoohPe4O6C%2Fimage.png?alt=media&amp;token=a47dd6e0-5c84-4e8f-8a5a-0d93f5733bc0" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FlHUVjFrWkZHUj7NuH2JR%2Fimage.png?alt=media&amp;token=c19ad009-f2b8-4013-8126-bcf39e78b17e" alt=""><figcaption><p>Rollbar listing the tracing entry.</p></figcaption></figure>

<figure><img src="https://2555313172-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSUTvba76C6ObDoYtcvGJ%2Fuploads%2FKzBPxR2ATBtRX7kAGPOR%2Fimage.png?alt=media&amp;token=8dfb4c2f-32be-481b-9dce-f6cbf38fa237" alt=""><figcaption><p>A closer look into the details of an error span.</p></figcaption></figure>
