Example #1
0
        private static async Task Main(string[] args)
        {
            // Get user credentials from environment variables.
            //      Environment is used for this example because it makes it easier to rest the library.
            var username = Environment.GetEnvironmentVariable("USERNAME");
            var password = Environment.GetEnvironmentVariable("PASSWORD");

            // Initialize cache.
            //      This makes sure there is consistency between device identifiers per user and gets rid of
            //      repeated authentications.
            var cacheDirectory = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "Cache");
            var cacheHandler   = new FileCacheHandler(cacheDirectory);

            // Create a musical.ly client.
            //      Using to dispose properly.
            using (var client = new MusicallyClient(username, cacheHandler))
            {
                DiscoverUserMe profile = null;

                // Check if this instance is not logged in yet.
                //      ALWAYS do this check, because the access token gets cached and loaded when the client is created.
                if (!client.IsLoggedIn())
                {
                    var login = await client.LoginAsync(password);

                    if (login.success)
                    {
                        profile = await client.DiscoverUserMeAsync();
                    }
                }
                else
                {
                    profile = await client.DiscoverUserMeAsync();
                }

                // Sanity check.
                if (profile == null)
                {
                    throw new Exception("Profile was null..?");
                }

                // We are now properly authenticated.
                if (client.IsLoggedIn())
                {
                    Console.WriteLine($"Logged in as {profile.result.displayName}.");

                    // Do stuff with client.*
                }
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
        /// <summary>
        ///     Signs the request, must be done as last.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="api"></param>
        /// <returns></returns>
        public static IFlurlRequest WithSignHeaders(this IFlurlRequest request, MusicallyClient api)
        {
            var requestId   = Guid.NewGuid().ToString();
            var requestInfo = JsonConvert.SerializeObject(new RequestInfo
            {
                Os               = "android 8.0.0",
                Version          = "6.9.0",
                SliderShowCookie = string.Empty,
                XRequestId       = requestId,
                Url              = request.Url.ToString(),
                OsType           = "android",
                DeviceId         = api.Cache.Device.DeviceId,
                Timestamp        = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });

            var requestInfoEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(requestInfo));
            var requestSignature   = api.ApiSignature.GetSignature(requestInfoEncoded, api.Cache.Device.DeviceId).GetAwaiter().GetResult();

            return(request
                   .WithHeader("X-Request-ID", requestId)
                   .WithHeader("X-Request-Info5", requestInfoEncoded)
                   .WithHeader("X-Request-Sign5", requestSignature.Sign));
        }