Struggling with HttpUtils2 and Google API

keirS

Well-Known Member
Licensed User
Longtime User
Trying to add a column to a Google Fusion table and keep on getting "NOT FOUND" error message.

Sub to Add Column:

B4X:
Sub InsertColumn(oReturnObject As Object,cReturnSub As String,cTableName As String,cColumnName As String,cColumnType As String)
    Dim oTableToAddColumnTo As FusionTable
   Dim oJSONGenerator As JSONGenerator
   Dim mColumnMap As Map
   Dim oCallBack As CallBackClass
   Dim cColumnLink As String
    Dim cPostString As String
   Dim oHttpJob As HttpJob
   Dim oStringUtils As StringUtils
   Dim mHeaders As Map
   
   
   
   mHeaders.Initialize
   mColumnMap.Initialize
 If mTables.ContainsKey(cTableName) Then
     oCallBack.Initialize(oReturnObject,cReturnSub,"")
     oTableToAddColumnTo = mTables.Get(cTableName) 
    mColumnMap.Put("Name",cColumnName)
    mColumnMap.Put("Type",cColumnType)
    oJSONGenerator.Initialize(mColumnMap)
    mJobMap.Put("InsertColumn",oCallBack)
    cColumnLink = cTableLink & "/" & oTableToAddColumnTo.fusTableId & "/coulmns?key=" & CMyApiKey
    cPostString = oJSONGenerator.ToString
    mHeaders.Put("Authorization",cAuthToken)
    mHeaders.Put("Content-Type","application/json")
    oHttpJob.Initialize("InsertColumn",Me)
    Log(cColumnLink)
    Log(cPostString)
    oHttpJob.PostString2(cColumnLink,cPostString,mHeaders)
  End If

End Sub

Because the post request requires headers I added two new subs into HttpJob. I pass a Map of the header values and the use SetHeader to set the values.


B4X:
Public Sub PostString2(Link As String, Text As String,mHeaders As Map)
        PostBytes2(Link, Text.GetBytes("UTF8"),mHeaders)
        

End Sub

Public Sub PostBytes2(Link As String, Data() As Byte,mHeaders As Map)
   Dim cKeys As String
   mLink = Link
   req.InitializePost2(Link, Data)
   For  Each cKey In mHeaders.Keys 
      If cKey = "Content-Type" Then
         req.SetContentType(mHeaders.Get(cKey))
      Else
         req.SetHeader(cKey,mHeaders.Get(cKey))
      End If
      
   Next
   
   CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

I know the table exists because I cut and pasted into https://developers.google.com/fusiontables/docs/v1/reference/column/insert Try it section at the bottom and that gives me the following post request:

B4X:
POST https://www.googleapis.com/fusiontables/v1/tables/1JBBxm7njSjzSP--pXBDsz5PpoYUgn0Zl4ufNmyE/columns?key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  Bearer ya29.AHES6ZTAg8DmbmjZx98_oVsKuIrML_qgzZh0JJp_zrgEBIZHoMbmZhM
X-JavaScript-User-Agent:  Google APIs Explorer
 
{
 "name": "test",
 "type": "STRING"
}

I know my Auth key is ok because I just used it to get a list of tables. I really can't work out where I am going wrong.
 

keirS

Well-Known Member
Licensed User
Longtime User
OK with a bit fiddling I manged to trap the post request. It's not quite the same as the Google one because I don't have a trusted peer certificate so I can't decrypt SSL. I am using a proxy to capture the traffic.

B4X:
POST /fusiontables/v1/tables/1JBBxm7njSjzSP--pXBDsz5PpoYUgn0Zl4ufNmyE/coulmns?key=AIzaSyCvqjbYyP6aLJBwinXcslrBql9RBLKejYo HTTP/1.1
Authorization: Bearer ya29.AHES6ZQlUvIz2pDAVHVGQZQ2UFh4J4dTIP4mQd2N_iQEIQ5orPMq
Content-Length: 43
Content-Type: application/json
Host: www.googleapis.com
Connection: Keep-Alive

{
   "type": "STRING",
   "name": "Stock"
}

Now I am not an expert on HTTP but to my eye they look pretty similar :BangHead:


Any suggestion gratefully accepted :sign0163:
 
Last edited:
Upvote 0
Top