Course 2 - Week 2 - Lab2 - Batch Data Processing from API

I’m having some doubts about this block of code:

if "None" in None:
                    # Call get_auth_header() function with the "access_token" from the token_response.
                    headers = None(
                        access_token=None["None"]
                    )
                    print("Token has been refreshed")
                    continue  # Retry the request with the updated token
                else:
                    print("Failed to refresh token.")
                    return []

I’m not understanding the “None” in “” and also the headers part, can someone please give me a tip?

Thank you in advance for your time.

Ricardo.

Hello @Ricardo_Lousada,
This is the same step as before but this time your are refreshing your auth_header if you get a 401 error. The 401 error means that your access_token has expired after 60minutes:

if the access_token in the token_response you get in the previous line, then continue:

Call the get_auth_header() function with the access_token[“access_token”] from the token_response.

Hope its helpful

Hi @Georgios, thanks for the help, I made it :slight_smile:

@Ricardo_Lousada glad it fixed, thanks

1 Like

getting error as “AttributeError: ‘dict’ object has no attribute ‘albums’“ at # Use extend() method to add the albums’ items to the list of responses.

\# responses.None(response.None('None').None('None'))

responses.extend(response.albums('None').items('None'))

not sure what update needed here

error details:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[42], line 1 ----> 1 responses = paginated_new_releases(endpoint_request=get_new_releases, 2 url=URL_NEW_RELEASES, 3 access_token=token.get(‘access_token’), 4 offset=0, limit=20) Cell In[41], line 32, in paginated_new_releases(endpoint_request, url, access_token, offset, limit) 29 response = endpoint_request(**kwargs) 30 # Use extend() method to add the albums’ items to the list of responses. 31 # responses.None(response.None(‘None’).None(‘None’)) —> 32 responses.extend(response.albums(‘None’).items(‘None’)) 33 # .items(‘None’)) 34 # Get the total number of the elements in albums and save it in the variable total_elements. 35 total_elements = responses.albums(‘None’).total(‘None’)

Code:
{mentor edit: code removed}

Hello @vijaybhangale

Your code contains a lot of “None"s. You are supposed to follow the instructions to replace these with the correct values and run the code cell afterwards.

Hi @Amir_Zare : I update all “None” values.

7th instruction in the lab is

Inside the `while` loop do the following steps:

* Update the `offset` value with the current value from the request you did plus the `limit` value.

What is mean by “current value“ here. There is no “current value” in the response mentioned @ Web API Reference | Spotify for Developers

My assignment says “Deadline Pass this assignment by Sep 11, 1:59 AM CDT”, what this deadlight means. If I don’t complete the assignment by 11th I will fail the course, I need to restart the course from the scratch or I need to redo whole week material again …

The deadlines mean very little. They’re just reminders. You can ignore them, and they will reset automatically as necessary.

execution if going into infinite loop, always pulling first 20 elements with offset = 0 and limit =20, offset value is not increasing in the loop. Any hint why it is going to infinite loop

{mentor edit: code removed}

Hello @vijaybhangale,

Could you check in Exercise 1 you are using the correct url (url=request_url). Hope that helps with offset not updating.

Hello @vijaybhangale

Also please avoid sharing you code, could you remove it. Thank you

if “access_token” in token_response:
headers = get_auth_header(
access_token=token_response[“access_token”]
)
print(“Token has been refreshed”)
continue
else:
print(“Failed to refresh token.”)
return

  1. if "access_token" in token_response:
  • This checks if the dictionary token_response contains the key "access_token".

  • If it does, that means the token refresh worked and you got a new token.


2. headers = get_auth_header(access_token=token_response["access_token"])

  • Extracts the new access token: token_response["access_token"].

  • Calls get_auth_header() with that token.

  • Stores the result in headers.
    (Most likely, this builds the HTTP headers with the refreshed token.)


3. print("Token has been refreshed")

  • Just a confirmation message that the refresh worked.

4. continue

  • Skips the rest of the loop and retries the request using the new token.

5. else:

  • Runs if "access_token" is not found in token_response.

6. print("Failed to refresh token.")

  • Prints an error message.

7. return []

  • Exits the function and returns an empty list when the token couldn’t be refreshed.

1 Like