Android Question Google sheets API Key error

IndieDev

Active Member
Licensed User
Hi,

Trying to access a Google Sheet using OAUTH v2.10

Getting this error...

B4X:
ResponseError. Reason: , Response: {
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "sheets.googleapis.com"
        }
      }
    ]
  }
}

My Code:
B4X:
Dim sSTR As String = "https://sheets.googleapis.com/v4/spreadsheets/" & sheetID & "?ranges=" & range 

    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        ToastMessageShow("Error accessing account.", True)
        Return
    End If
    
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download2(sSTR, Array As String("key", API_KEY))
    j.GetRequest.SetHeader("Authorization", "Bearer " & Token)
    
    If j.Success Then
        Log(j.GetString)
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim values As List = root.Get("values")
        Dim cindex, rindex As Int
        
        Spn_Line.clear
        For Each rowvalues As List In values    'get row
            rindex=rindex+1
            'Log("row: " & rowvalues)

            For Each colvalues As String In rowvalues    'get column
                cindex=cindex+1
                Spn_Line.Add(colvalues)
                Log(colvalues)
                Log("rindex: " & rindex & "cindex: "&cindex)
            Next
            cindex = 0
        Next
    End If
    j.Release

What am I doing wrong?
 

IndieDev

Active Member
Licensed User
Tried with this string now...
B4X:
    Dim sSTR As String = "https://sheets.googleapis.com/v4/spreadsheets/" & sheetID & _
                        "?includeGridData=true&ranges=" & range
    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        ToastMessageShow("Error accessing account.", True)
        Return
    End If
   
    Dim j As HttpJob
    j.Initialize("", Me)
   
    j.Download2(sSTR, Array As String("key", API_KEY))
    j.GetRequest.SetHeader("Authorization", "Bearer " & Token)

Getting error...
B4X:
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: , Response: {
  "error": {
    "code": 400,
    "message": "Unable to parse range: LIST!i2:115?key=[API_KEY]",
    "status": "INVALID_ARGUMENT"
  }
}
 
Last edited:
Upvote 0

IndieDev

Active Member
Licensed User
Ok. I changed my query to this.... (minus the API_KEY)
B4X:
Dim sSTR As String = "https://sheets.googleapis.com/v4/spreadsheets/" & sheetID & "?includeGridData=true&ranges=" & range

    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        ToastMessageShow("Error accessing account.", True)
        Return
    End If
  
    Dim j As HttpJob
    j.Initialize("", Me)
j.Download(sSTR)

My Log displays the results.

But, I get this error ...
B4X:
java.lang.RuntimeException: Object should first be initialized (List).
for the line...
B4X:
For Each rowvalues As List In values    'get row
 
Last edited:
Upvote 0

IndieDev

Active Member
Licensed User
I think I've already initialized the List...
B4X:
    If j.Success Then
        Log(j.GetString)
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim sValues As List = root.Get("values")
        Dim cindex, rindex As Int
        
        Spn_Line.clear
        For Each rowvalues As List In sValues    'get row
            rindex=rindex+1
            'Log("row: " & rowvalues)

            For Each colvalues As String In rowvalues    'get column
                cindex=cindex+1
                Spn_Line.Add(colvalues)
                Log(colvalues)
                Log("rindex: " & rindex & "cindex: "&cindex)
            Next
            cindex = 0
        Next

'        Spn_Line.clear
'        For Each nm As NameAndMac In btDevices
'            Spn_Line.Add(nm.name)
'        Next

    End If
    j.Release

Where am I going wrong?

Any help is appreciated.
 
Upvote 0

IndieDev

Active Member
Licensed User
OK. I changed my JSON parser to this....
B4X:
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim root As Map = parser.NextObject
        Dim sheets As List = root.Get("sheets")
        For Each colsheets As Map In sheets
            Dim data As List = colsheets.Get("data")
            For Each coldata As Map In data
                Dim rowData As List = coldata.Get("rowData")
                For Each colrowData As Map In rowData
                    Dim values As List = colrowData.Get("values")
                    For Each colvalues As Map In values
                        Dim effectiveValue As Map = colvalues.Get("effectiveValue")
                        Dim stringValue As String = effectiveValue.Get("stringValue")
                        Spn_Line.Add(stringValue)
                    Next
                Next
            Next
        Next

Here "Spn_Line" is a spinner.

Getting error
B4X:
java.lang.RuntimeException: Object should first be initialized (Map).
on this line....
B4X:
Dim stringValue As String = effectiveValue.Get("stringValue")
 
Upvote 0

IndieDev

Active Member
Licensed User
Finally Sorted!!!

Changed the error line code to check if "effectiveValue" has been initialized...
B4X:
                        Dim effectiveValue As Map = colvalues.Get("effectiveValue")
                        If effectiveValue.IsInitialized Then
                            Dim stringValue As String = effectiveValue.Get("stringValue")
                            Spn_Line.Add(stringValue)
                        End If

Leaving this post in case it helps someone (like me :))
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Want to mark this thread as SOLVED.

How to do it?
Edit the title of the post and put [SOLVED]

 
Upvote 0

IndieDev

Active Member
Licensed User
Hi Asales,

Thanks for your reply.

I've read that thread earlier too.
But, unfortunately, I cannot find the option to edit the Title of my post.
The attached screenshot is what is visible to me......

Please be kind as to mention the steps to do the same.

Regards
 

Attachments

  • Screenshot (22).png
    Screenshot (22).png
    77.3 KB · Views: 210
Upvote 0
Top