Xojo.Net.HTTPSocket: FamilySearch API

Hi everyone,

i created successfully my FamilySearch-Developer Account. Than, i made a Subclass of Xojo.Net.HTTPSocket called FamilySearchAPI.
[h]FamilySearchAPI[/h]
Constants

  • ClientID As Text // My Developer ID
  • GrantType As Text = “password”
  • Password As Text // My Developer Password
  • URL As Text = “https://sandbox.familysearch.org
  • URL_Search As Text = “/platform/tree/search”
  • URL_Token As Text = “/cis-web/oauth2/v3/token”
  • User As Text // My Developer Username
    Properties
  • AccessToken As Text
  • Dict As Xojo.Core.Dictionary

Then i add these class to a Window and add a Button and TextArea. Buttons Action Event:

[code]Using Xojo.Core
Using Xojo.Data

Dim FamilySearch_Access As Text
Dim header As Dictionary
Dim json As Text
Dim data As MemoryBlock

FamilySearch_Access = “?username=” + FamilySearchAPI.User _
+ “&grant_type=” + FamilySearchAPI.GrantType _
+ “&client_id=” + FamilySearchAPI.ClientID _
+ “&password=” + FamilySearchAPI.Password

header = New Dictionary
header.Value(“Accept”) = “application/json”

json = GenerateJSON(header)

data = TextEncoding.UTF8.ConvertTextToData(json)

FamilySearchAPI.SetRequestContent(data, “application/x-www-form-urlencoded”)
FamilySearchAPI.Send(“POST”, FamilySearchAPI.URL + FamilySearchAPI.URL_Token + FamilySearch_Access)[/code]
The PageReceived-Event of the FamilySearchAPI-Object:

[code]Dim jsonData As Text
jsonData = Xojo.Core.TextEncoding.UTF8.ConvertDataToText(Content)

Me.dict.RemoveAll
Me.dict = Xojo.Data.ParseJSON(jsonData)

If Me.dict.HasKey(“access_token”) Then
Me.AccessToken = Me.dict.Value(“access_token”)
End If

TextArea1.Text = jsonData + EndOfLine + EndOfLine + HTTPStatus.ToText[/code]

All Codes above working fine! But now, i wanna use FamilySearch’s Person Search.
Request

GET /platform/tree/search?q=motherGivenName:Clarissa+fatherSurname:Heaton+motherSurname:Hoyt+surname:Heaton+givenName:Israel+fatherGivenName:Jonathan Accept: application/x-gedcomx-atom+json Authorization: Bearer YOUR_ACCESS_TOKEN_HERE
I added a second Button and his Action-Event looks like this:

[code]Using Xojo.Core
Using Xojo.Data

Dim FamilySearch_Access As Text

FamilySearch_Access = “?q=motherGivenName:Clarissa+fatherSurname:Heaton+motherSurname:Hoyt+surname:Heaton+givenName:Israel+fatherGivenName:Jonathan”

Dim header As New Dictionary
header.Value(“Accept”) = “application/x-gedcomx-atom+json”
header.Value(“Authorization”) = "Bearer " + FamilySearchAPI.AccessToken

Dim json As Text
json = GenerateJSON(header)

Dim data As MemoryBlock
data = TextEncoding.UTF8.ConvertTextToData(json)

FamilySearchAPI.SetRequestContent(data, “application/x-www-form-urlencoded”)

Dim outputFile As Xojo.IO.FolderItem = Xojo.IO.SpecialFolder.Documents.Child(“data.json”)
FamilySearchAPI.Send(“GET”, FamilySearchAPI.URL + FamilySearchAPI.URL_Search + FamilySearch_Access, outputFile)[/code]
And this returns noting. Only a nil JSON-File :confused: Why? It should look like the response on the FamilySearch-API JSON-Sample above.

How to use Xojo.Net.HTTPSocket to get Data?

Thank you all.

In the PageReceived handler for that second API call (the one to FamilySearch’s Person Search), what are you getting for the HTTPStatus value?

Hi Tim, i got nothing :confused:

Is it possible, i need to use other HTTPSocket Methods when i use “GET”?

The problem might be that in your second API call (which is doing a GET request) you aren’t passing header values.

Try adding this (before your FamilySearchAPI.Send call)…

FamilySearchAPI.RequestHeader( “Accept” ) = “application/x-gedcomx-atom+json”
FamilySearchAPI.RequestHeader( “Authorization” ) = "Bearer " + FamilySearchAPI.AccessToken

Sure, i set the header values, look:

[code]Dim header As New Dictionary
header.Value(“Accept”) = “application/x-gedcomx-atom+json”
header.Value(“Authorization”) = "Bearer " + FamilySearchAPI.AccessToken

Dim json As Text
json = GenerateJSON(header)

Dim data As MemoryBlock
data = TextEncoding.UTF8.ConvertTextToData(json)

FamilySearchAPI.SetRequestContent(data, “application/x-www-form-urlencoded”)[/code]
Or is that the wrong way?

Have you set the user-agent ?

@Martin Trippensee: You’re trying to pass the headers in the body. However, you’re doing a GET request, so it’s possible that the FamilySearch API isn’t looking in the body for the headers (like it is when you’re doing a POST).

Again, I would try setting the headers as true request headers, like this:

FamilySearchAPI.RequestHeader( “Accept” ) = “application/x-gedcomx-atom+json”
FamilySearchAPI.RequestHeader( “Authorization” ) = "Bearer " + FamilySearchAPI.AccessToken

Also, in the PageReceived handler, you should be getting a value for HTTPStatus, regardless of whether the request was successful or not. It seems odd that you aren’t getting anything. That value might help to shed some light on the problem.

Thanks Tim, thats the solution :slight_smile: