question

My Studio avatar image
My Studio asked

How to implement API codes to unity

Dear Playfab developers,

First of all sorry for such a "Stupid" question but if this is answered I can continue developing my game.

My question is as following:

When I read https://api.playfab.com/docs/getting-started-with-playfab there you can see good c# codes which I can implement in my game and it works.

But if I search for loginwithfacebook I find the following: https://api.playfab.com/Documentation/Client/method/LoginWithFacebook

There you see this example:

Sample Request

POST https://{
                 {TitleID}}.playfabapi.com/Client/LoginWithFacebook
Content-Type: application/json;
{
  "TitleId": "1",
  "AccessToken": "FaceAccessTokenID",
  "CreateAccount": false
}

How can I convert this to c# code to use it with my game? Any tutorials on this? 

 

Thanks in advance,

Speedt4

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Your best bet would be to download and use our Unity SDK, which contains all the API methods, wrapped up as C# calls in Unity: https://api.playfab.com/sdks/unity.

All our calls are Web API under the covers, which is why our core documentation describes them at that level. All the calls are then wrapped in a simple pattern for our SDKs, with success/failure callbacks wherever possible. The SDKs also take care of JSON serialization/deserialization and making the HTTP calls.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

My Studio avatar image
My Studio answered

I have downloaded the SDK, I am testing the following to login with facebook:

 

void Start()
{
FB.Init();
PlayFabSettings.TitleId = "/*I hide this for this forum*/";
Login_FB(PlayFabSettings.TitleId);
}

void Login_FB(string titleId)
{
LoginWithFacebookRequest request = new LoginWithFacebookRequest()
{
TitleId = titleId,
CreateAccount = true,
AccessToken = AccessToken.CurrentAccessToken.ToString()
};

PlayFabClientAPI.LoginWithFacebook(request, (result) => {
PlayFabId = result.PlayFabId;

Debug.Log("Got PlayFabID: " + PlayFabId);

if (result.NewlyCreated)
{
Debug.Log("(new account)");
}
else
{
Debug.Log("(existing account)");
}
},
(error) => {
Debug.Log("Error logging in player with Facebook:");
Debug.Log(error.ErrorMessage);
});
}

I don't get compile errors but when I start my game I get the following error:

NullReferenceException: Object reference not set to an instance of an object
PlayFabManager.Login_FB (System.String titleId) (at Assets/Scripts/Networking scripts/PlayFabManager.cs:21)
PlayFabManager.Start () (at Assets/Scripts/Networking scripts/PlayFabManager.cs:16)

So that means that I am doing something wrong with the "PlayFabSettings.TitleId"

 

Can you maybe push me in the right direction?

 

With kind regards, 

Speedt4

 

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

Actually, it means that your function Login_FB(System.String titleId) - the signature of the function call - has a null reference exception on or around line 21 of the file. I suspect the issue is this line:

AccessToken = AccessToken.CurrentAccessToken.ToString() 

There is no CurrentAccessToken string as a member of AccessToken. AccessToken itself is simply a string. Can you have a look at how you're retrieving and using the Facebook token in your code?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.