question

Martin Crockford avatar image
Martin Crockford asked

Trying to download json content from CDV

I get the following exception while trying to download from CDN

PlayFabException: Must be logged in to call this method PlayFab.PlayFabClientAPI.GetContentDownloadUrl (PlayFab.ClientModels.GetContentDownloadUrlRequest request, System.Action`1 resultCallback, System.Action`1 errorCallback, System.Object customData, System.Collections.Generic.Dictionary`2 extraHeaders) (at Assets/PlayFabSDK/Client/PlayFabClientAPI.cs:409) PlayFabCDN.GetDownloadUrl (System.String key, System.Action`1 onComplete) (at Assets/Scripts/PlayFab/PlayFabCDN.cs:21) PlayFabCDN.DownloadFileFromCDN (System.String key) (at Assets/Scripts/PlayFab/PlayFabCDN.cs:13) WorldMaker.Start () (at Assets/Scripts/WorldMaker.cs:44)


Download code as below

Login and Download

I have been working on this for a couple of days now and can't get past it - any help would be very much appreciated. (The player seems to be logged in and their ID has been made in Playfab)

 public static void Login()
    {
        if (string.IsNullOrEmpty(PlayFabSettings.staticSettings.TitleId))
        {
            /*
            Please change the titleId below to your own titleId from PlayFab Game Manager.
            If you have already set the value in the Editor Extensions, this can be skipped.
            */
            PlayFabSettings.staticSettings.TitleId = "EB9E4";
        }
        var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
        PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);


       
    }


    private static void OnLoginSuccess(LoginResult result)
    {
        Debug.Log("Congratulations, you made your first successful API call!");
    }


    private static void OnLoginFailure(PlayFabError error)
    {
        Debug.LogWarning("Something went wrong with your first API call.  :(");
        Debug.LogError("Here's some debug information:");
        Debug.LogError(error.GenerateErrorReport());
    }


  public static void DownloadFileFromCDN(string key)
    {
        GetDownloadUrl(key, presignedUrl =>
        {
            Console.WriteLine("");
        });
    }


    static void GetDownloadUrl(string key, Action<string> onComplete)
    {
        PlayFabClientAPI.GetContentDownloadUrl(new GetContentDownloadUrlRequest()
        {
            Key = key,
            ThruCDN = true
        }, result => onComplete(result.URL),
        error => Debug.LogError(error.GenerateErrorReport()));
    }
Content
10 |1200

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

Sarah Zhang avatar image
Sarah Zhang answered

PlayFab Unity SDK makes API calls using Unity Coroutine, which is asynchronous, so you need to call the PlayFabClientAPI.GetContentDownloadUrl in the callback function of PlayFabClientAPI.LoginWithCustomID to ensure you call it after you logged players in. For Example, you can call this method -- GetDownloadUrl(string key, Action<string> onComplete) in the OnLoginSuccess function, like the following code shows.

    ...
    private void OnLoginSuccess(LoginResult result)
    {
        //Call GetDownloadUrl function here
        GetDownloadUrl(customKey, OnSuccess);
        Debug.Log("Congratulations, you made your first successful API call!");
    }
    ...
2 comments
10 |1200

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

Martin Crockford avatar image Martin Crockford commented ·

Many thanks will try this today

0 Likes 0 ·
Martin Crockford avatar image Martin Crockford commented ·

Ok so got a bit further, I am just a beginner with Playfab so things a very challenging at the moment

The class now looks like this.

playfabhelpers.txt

but there is an issue with the Onsucess -not sure how to solve that, any help would be much appreciated

Apparently it doesn't exist in the current context

0 Likes 0 ·
playfabhelpers.txt (1.9 KiB)
playfab.png (131.5 KiB)
Martin Crockford avatar image
Martin Crockford answered

Ok so got a bit further, I am just a beginner with Playfab so things a very challenging at the moment

The class now looks like this.

playfabhelpers.txt

but there is an issue with the Onsucess -not sure how to solve that, any help would be much appreciated

Apparently it doesn't exist in the current context


playfab.png (131.5 KiB)
playfabhelpers.txt (1.9 KiB)
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.