TableFusion API NET Invalid Credentials [401]

58 views Asked by At

I am using this code found in StackOverflow to manage FusionTable:

API:

Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Fusiontables.v2
Imports Google.Apis.Services
Imports Google.Apis.Upload

Credential:

Dim scopes() As String = New String() {"oauth2:https://www.googleapis.com/auth/fusiontables"} 
Dim creds As GoogleCredential = GoogleCredential.FromStream(File.OpenRead("MYJASON.json")) 
creds.CreateScoped(scopes)

I tried with:

Dim scopes() As String = New String() {FusiontablesService.Scope.Fusiontables}

But same result.

Then

Dim service As FusiontablesService = New FusiontablesService(New BaseClientService.Initializer With {
            .HttpClientInitializer = creds,
            .ApplicationName = "MYAPPLICATION"
})

Then

Dim result As TableResource = service.Table
If result IsNot Nothing Then
    Try
        Dim str As StreamReader = New StreamReader("MYFILE.csv")

        Dim UpStatus As IUploadProgress = service.Table.ReplaceRows("MYTABLE", str.BaseStream, "application/octet-stream").Upload()
        If UpStatus.Status = Google.Apis.Upload.UploadStatus.Failed Then
        Console.WriteLine(UpStatus.Exception)
        End If
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
Else
    Console.WriteLine("Error Initializer")
End If

Error at :

service.Table.ReplaceRows
UpStatus.Status = Google.Apis.Upload.UploadStatus.Failed

ex.Message:

Google.Apis.Requests.RequestError Invalid Credentials [401]

What's wrong??

I have an API Key, 2 ID clients OAuth 2.0, and 1 service ID, nothing works.

1

There are 1 answers

0
Steph On

here the soluce, found in parte in stackoverflow

1°) Create a Service Account Keys credencial
2°) Download the JSON FIle
3°) generate a c# class. You can using (http://json2csharp.com/)

In my case the class name is "JsonFileProperties"

4°) Use this code

 Dim scopes() As String = New String() {FusiontablesService.Scope.Fusiontables}
 Dim json As String = File.ReadAllText("MY.json")
 Dim JsonProperties As JsonFileProperties = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JsonFileProperties)(json)
 Dim credential = New ServiceAccountCredential(New ServiceAccountCredential.Initializer(JsonProperties.client_email) With {
                .Scopes = scopes
                }.FromPrivateKey(JsonProperties.private_key))

 Dim service As FusiontablesService = New FusiontablesService(New BaseClientService.Initializer With {
                .HttpClientInitializer = credential,
                .ApplicationName = "*******",
                .ApiKey = "MYAPIKEY"
            })

5°) For access to edit o update tables (edit, delete, importraws, etc) not forget to allow the mail present in the JSON file in the list of autorized user to the FusionTable