Skip to content

FileNotFoundException on first authentication attempt #43

@Peaj

Description

@Peaj

File.ReadAllText in LoadPKCEToken fails on first authentication attempt because the file has not been created yet.
The code only checks if the file is empty but throws an exception if it does not exist.
A simple File.Exists check should fix it.

Tested in Unity Editor version 2021.1.12f2

FileNotFoundException: Could not find file "G:\Unity\Projects\Spotify4Unity\Spotify4Unity\PKCE-credentials.json"
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) (at <695d1cc93cca45069c528c15c9fdd749>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize, System.Boolean checkHost) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.IO.StreamReader..ctor (System.String path, System.Boolean detectEncodingFromByteOrderMarks) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.IO.StreamReader..ctor (System.String path) (at <695d1cc93cca45069c528c15c9fdd749>:0)
(wrapper remoting-invoke-with-check) System.IO.StreamReader..ctor(string)
System.IO.File.ReadAllText (System.String path) (at <695d1cc93cca45069c528c15c9fdd749>:0)
PKCE_Authentification.LoadPKCEToken () (at Assets/Spotify4Unity/Core/Managed Authentification/PKCE/PKCE_Authentification.cs:253)
PKCE_Authentification.HasPreviousAuthentification () (at Assets/Spotify4Unity/Core/Managed Authentification/PKCE/PKCE_Authentification.cs:123)
PKCE_Authentification.StartAuthentification () (at Assets/Spotify4Unity/Core/Managed Authentification/PKCE/PKCE_Authentification.cs:65)
SpotifyService.AuthorizeUser () (at Assets/Spotify4Unity/Core/Services/SpotifyService.cs:159)
AccountStatusController.OnSignIn () (at Assets/Spotify4Unity/Examples/Scripts/Landing Scene/AccountStatusController.cs:45)
AccountStatusController.<Start>b__3_0 () (at Assets/Spotify4Unity/Examples/Scripts/Landing Scene/AccountStatusController.cs:25)
UnityEngine.Events.InvokableCall.Invoke () (at <adfa4b62400849189388df71c9e26e89>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <adfa4b62400849189388df71c9e26e89>:0)
UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions