Exemple #1
0
        public void graphApi(string options)
        {
            var args        = JsonHelper.Deserialize <List <string> >(options);
            var apiPath     = args[0];
            var permissions = JsonHelper.Deserialize <string[]>(args[1]);

            Deployment.Current.Dispatcher.BeginInvoke(async() =>
            {
                if (string.IsNullOrEmpty(CurrentTokenData.AccessToken))
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "No active sessions found"));
                    return;
                }

                if (CurrentTokenData.NeedNewPermissions(permissions))
                {
                    var permError = new PermissionsError
                    {
                        Code = "permissions_missing",
                        Uri  =
                            await GetDialogUri("oauth", LOGIN_REDIRECT_URI, new Dictionary <string, object>
                        {
                            { "auth_type", "rerequest" },
                            { "response_type", LOGIN_RESPONSE_TYPE },
                            { "scope", string.Join(",", permissions) }
                        })
                    };

                    DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, permError));
                    return;
                }

                await Session.CheckAndExtendTokenIfNeeded();

                try
                {
                    dynamic result = await FbClient.GetTaskAsync(apiPath);
                    DispatchCommandResult(new PluginResult(PluginResult.Status.OK, result.ToString()));
                }
                catch (Exception ex)
                {
                    DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message));
                }
            });
        }
Exemple #2
0
        // ReSharper restore InconsistentNaming, UnusedMember.Global

        #region Private methods

        /// <summary>
        /// Updates an AccessTokenData object with data fetched from FB API.
        /// Updates ActiveSession with updated AccessTokenData object.
        /// Uses CurrentAccessTokenData if parameter is null
        /// </summary>
        /// <param name="tempToken">AccessTokenData object to update</param>
        /// <returns>LoginStatus object</returns>
        private static Task <LoginStatus> UpdateAndGetLoginStatus(AccessTokenData tempToken = null)
        {
            var tcs = new TaskCompletionSource <LoginStatus>();

            Deployment.Current.Dispatcher.BeginInvoke(async() =>
            {
                if (tempToken != null)
                {
                    CurrentTokenData = tempToken;
                }

                if (string.IsNullOrEmpty(CurrentTokenData.AccessToken))
                {
                    tcs.SetResult(new LoginStatus {
                        Status = "unknown"
                    });
                    return;
                }

                await Session.CheckAndExtendTokenIfNeeded();

                if (CurrentTokenData.CurrentPermissions.Count == 0 || string.IsNullOrEmpty(CurrentTokenData.FacebookId))
                {
                    // Create a copy of existing access token data to update it with new values
                    var newTokenData = CurrentTokenData.Clone();

                    try
                    {
                        var result =
                            (JsonObject)
                            await
                            FbClient.GetTaskAsync("debug_token",
                                                  new { input_token = CurrentTokenData.AccessToken });

                        var data = (JsonObject)result.ToDictionary(pair => pair.Key, pair => pair.Value)["data"];

                        var userId = (string)data.ToDictionary(pair => pair.Key, pair => pair.Value)["user_id"];
                        newTokenData.FacebookId = userId;

                        var actualPermissions = (JsonArray)data.ToDictionary(pair => pair.Key, pair => pair.Value)["scopes"];
                        foreach (var actualPermission in actualPermissions)
                        {
                            newTokenData.CurrentPermissions.Add((string)actualPermission);
                        }

                        Session.ActiveSession.CurrentAccessTokenData = newTokenData;
                    }
                    catch
                    {
                        // No need to fail here, just return a loginStatus object without userID
                    }
                }

                var loginStatus = new LoginStatus
                {
                    Status       = "connected",
                    AuthResponse = new AuthResponse
                    {
                        AccessToken = CurrentTokenData.AccessToken,
                        ExpiresIn   = CurrentTokenData.Expires,
                        UserId      = CurrentTokenData.FacebookId
                    }
                };

                tcs.SetResult(loginStatus);
            });

            return(tcs.Task);
        }