locked
Error while list data from youtube channel (problems happen occasionally) RRS feed

  • Question

  • User944339287 posted

    Hi guys.. below is my code to list data from my youtube channel.

    It's working fine all the time, but the error will be returned occasionally:
    The remote server returned an error: (403) Forbidden.

    And it will back to normal after a couple of times. Does anyone know why it will happen?

            Dim wc As WebClient = New WebClient With {
                .Encoding = Encoding.UTF8
            }
    
            Dim key As String = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    
            Try
                Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/playlists?part=snippet&key=" & key & "&maxResults=50&channelId=" & Me.txt_username.Text.Trim & "")
                Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject)
    
                For Each entry In jobj("items")
                    DT.Rows.Add(entry("snippet")("title").ToString(), entry("snippet")("description").ToString(), entry("id").ToString(), entry("snippet")("thumbnails")("medium")("url").ToString())
                Next
    
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try



    Tuesday, March 10, 2020 10:40 AM

Answers

  • User-1330468790 posted

    Hi, kengkit,

     

    Obviously, the problem is as the error message said.

    You really exceeded your quota for querying from Google API.

     

    Then you should upgrade your google account or wait for another day/week/month since you will have new quota. (as far as I know).

     

    I suggest you could mark the answer if you think it is right and helpful.

    This will help other people who faces the same issue to find the right answer faster.

     

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 13, 2020 9:01 AM

All replies

  • User475983607 posted

    Your code is a bit sloppy.  Maybe get the stack trace and inner exception along with the Error message in the catch block.  

    The URL has a direct dependency on user input.  Maybe the input is empty?  I do not see validation that stops the user from entering invalid data.

    Tuesday, March 10, 2020 10:51 AM
  • User944339287 posted
    Hi. Thanks for ur replied.

    The input is valid as it’s generated by system.

    Meanwhile.. do you mind to improve my code as it’s actually refer from online tutorials.

    Thank you
    Tuesday, March 10, 2020 1:02 PM
  • User475983607 posted

    The input is valid as it’s generated by system.

    The snippet of code you shared above is fetching an input value, Me.txt_username.Text.Trim, which is commonly user entry.  If this value is machine generated then there could be a problem with the code that generates the value.  

    Tuesday, March 10, 2020 1:21 PM
  • User-1330468790 posted

    Hi, kengkit,

     

    I can see that you are using "YouTube Data API v3" to fetch json data.

    It is not clear that what the exact reason is for your error. There is a list of the errors for this data API.

    https://developers.google.com/youtube/v3/docs/errors#gdata.CoreErrorDomain

    Could you check the error details and provide with more specified information?

     

    Guess:

    Since the error occurs occasionally, it should not be a problem of using 'WebClient' or simple access forbidden issue as below. 

    Access forbidden. The request may not be properly authorized.

    Possibile reasons:

    Error type Error detail Description
    forbidden (403) channelClosed The channel identified in the request has been closed.
    forbidden (403) channelNotFound The channel identified in the request cannot be found.
    forbidden (403) channelSuspended The channel identified in the request has been suspended.

    Above errors can be solved by checking the json string to see if the error message contains above details.

    Hope this can help you.

    Best regards,

    Sean

    Wednesday, March 11, 2020 2:51 AM
  • User944339287 posted

    Hi, The remote server returned an error: (403) Forbidden. is the only error message i can get in "immediate windows"

    meanwhile, i wonder is it because of my quota full? (i'm the only user to do testing, 5~8 visit per day. how come i can consumed all the quota?)



    Wednesday, March 11, 2020 3:34 PM
  • User-1330468790 posted

    Hi, kengkit,

    As far as I can see, you don't use up all of the quota so that the problem could not be it.

    Actually you can see the details from your side, the error message you found is just a brief description from the exception by 'try-catch'.

    Solution:

    Modify the try-catch to get more detailed information about the error.

    Dim wc As WebClient = New WebClient() With {
            .Encoding = Encoding.UTF8
        }
        Dim key As String = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    
        Try
            Dim jsonstring As String = wc.DownloadString("https://www.googleapis.com/youtube/v3/playlists?part=snippet&key=" & key & "&maxResults=50&channelId=" & Me.txt_username.Text.Trim & "")
            Dim jobj As JObject = CType(JsonConvert.DeserializeObject(jsonstring), JObject)
    
            For Each entry In jobj("items")
                DT.Rows.Add(entry("snippet")("title").ToString(), entry("snippet")("description").ToString(), entry("id").ToString(), entry("snippet")("thumbnails")("medium")("url").ToString())
            Next
    
        Catch ex As WebException
            Dim resp = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
            Dim obj As dynamic = JsonConvert.DeserializeObject(resp)
            Dim messageFromServer = obj.[error].message
            Response.Write(messageFromServer)
        End Try

    The resp here should be a json string like below:

    {
       "error":{
          "errors":[
             {
                "domain":"youtube.playlist",
                "reason":"channelClosed",
                "message":"The channel specified in the echannelId parameter has been closed.",
                "locationType":"parameter",
                "location":"channelId"
             }
          ],
          "code":403,
          "message":"The channel specified in the channelId parameter has been closed."
       }
    }

    Hope this can help you.

    Best regards,

    Sean

    Thursday, March 12, 2020 10:55 AM
  • User944339287 posted

    Hi i having difficulties to apply your code.

    Thursday, March 12, 2020 11:21 AM
  • User944339287 posted

    I have got the error message by using the following code:

    Reason: quotaExceeded
    Message: "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e."

                Dim resp = New StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
                Response.Write(resp)



    Friday, March 13, 2020 1:25 AM
  • User-1330468790 posted

    Hi, kengkit,

     

    Obviously, the problem is as the error message said.

    You really exceeded your quota for querying from Google API.

     

    Then you should upgrade your google account or wait for another day/week/month since you will have new quota. (as far as I know).

     

    I suggest you could mark the answer if you think it is right and helpful.

    This will help other people who faces the same issue to find the right answer faster.

     

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 13, 2020 9:01 AM