Skip to content

create_negative_keywords for Sponsored Brands - Method Raises ValueError in Requests Library #168

Closed

Description

@david-mcbacon

Describe the bug
I encountered a problem when trying to utilize the create_negative_keywords method from the Amazon Ads API library in Sponsored Brands Ads. Specifically, a ValueError: too many values to unpack (expected 2) is raised during the process of encoding parameters in the requests library. Other methods like list_negative_keywords work flawlessly, suggesting the problem is localized to the create_negative_keywords method.

Library version
python-amazon-ad-api==0.5.0

To Reproduce

def add_neg_kw_simple():
    payload = [
        {
            "adGroupId": 1234567890,
            "campaignId": 1234567890,
            "keywordText": "some kw text",
            "matchType": "negativeExact",
        }
    ]
    try:
        result = sb.NegativeKeywords(
            marketplace=Marketplaces.NA, credentials=my_credentials
        ).create_negative_keywords(body=payload)

        print(result)

    except AdvertisingApiException as e:
        print(e)

add_neg_kw_simple()

Console Output

Traceback (most recent call last):
  File "/Users/some/path/negative_kw_add.py", line 501, in <module>
    add_neg_kw_simple()
  File "/Users/some/path/negative_kw_add.py", line 491, in add_neg_kw_simple
    result = sb.NegativeKeywords(
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/ad_api/base/helpers.py", line 21, in wrapper
    return function(*args, **kwargs)
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/ad_api/api/sb/negative_keywords.py", line 76, in create_negative_keywords
    return self._request(kwargs.pop('path'), data=kwargs.pop('body'), params=kwargs)
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/ad_api/base/client.py", line 255, in _request
    res = request(
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/requests/sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/requests/sessions.py", line 484, in prepare_request
    p.prepare(
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/requests/models.py", line 371, in prepare
    self.prepare_body(data, files, json)
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/requests/models.py", line 559, in prepare_body
    body = self._encode_params(data)
  File "/opt/miniconda3/envs/amazon-ads/lib/python3.9/site-packages/requests/models.py", line 121, in _encode_params
    for k, vs in to_key_val_list(data):
ValueError: too many values to unpack (expected 2)

Desktop:

  • OS: MacOS Ventura
  • Version 13.5.2

Additional context

This error seems to arise when the requests library tries to encode the payload data.
As aforementioned, other methods (e.g., list_negative_keywords) work without issues, which indicates this issue is exclusive to the create_negative_keywords function.
I kindly ask for guidance on how to resolve this issue or any potential workarounds.

Thank you for your assistance!

Activity

  1. changed the title [-]`create_negative_keywords` Method Raises ValueError in Requests Library[/-] [+]`create_negative_keywords` for Sponsored Brands - Method Raises ValueError in Requests Library[/+] on Sep 21, 2023
  2. denisneuf commented on Oct 18, 2023

    @denisneuf
    Owner
    import json
    
    def add_neg_kw_simple():
        payload = [
            {
                "adGroupId": 1234567890,
                "campaignId": 1234567890,
                "keywordText": "some kw text",
                "matchType": "negativeExact",
            }
        ]
        try:
            result = sb.NegativeKeywords(
                marketplace=Marketplaces.NA, credentials=my_credentials
            ).create_negative_keywords(body=json.dumps(payload))
    
            print(result)
    
        except AdvertisingApiException as e:
            print(e)
    
    add_neg_kw_simple()
    

    Hi, sorry for late reply, this endpoint do not have the enhance body handling so you need to process:

    import json
    json.dumps(payload)

    list_negative_keywords method do not need to post a body in the request, I will add later the handler to make more easy and avoid this problems.

    Let me know if it works for you btw I move this to a discussion

  3. Repository owner locked and limited conversation to collaborators on Oct 18, 2023
  4. converted this issue into a discussion #174 on Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions