google-apiyoutube-analytics-api

Youtube Analytics API: Extracting User Activities by City but got an error


Hi I am using google api to pull User Activity by City. I followed the youtube doc User Activity by City

request = youtube_analytics.reports().query(
        dimensions="city",
        endDate="2024-08-31",
        ids="channel==MINE",
        maxResults=25,
        metrics="views",
        sort="views",
        startDate="2024-08-01"
    )
    response = request.execute()

but ive been getting this error:

{
  "error": {
    "code": 400,
    "message": "The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v2/available_reports for a list of supported queries.",
    "errors": [
      {
        "message": "The query is not supported. Check the documentation at https://developers.google.com/youtube/analytics/v2/available_reports for a list of supported queries.",
        "domain": "global",
        "reason": "badRequest"
      }
    ]
  }
}

I don't know whether I misspelled or anything but I checked everything and it is correct.


Solution

  • request = youtube_analytics.reports().query(
            dimensions="city",
            endDate="2024-08-31",
            ids="channel==MINE",
            maxResults=25,
            metrics="views",
            sort="-views",
            startDate="2024-08-01"
        )
        response = request.execute()
    

    Apparently, I need to spell '-views' at sort for it to work.