コード例 #1
0
  /**
   * Make a new Graph Request. View:
   * https://developers.facebook.com/docs/graph-api/reference/v2.4/request
   *
   * @param request
   * @param callback
   */
  public void newGraphRequest(
      GDXFacebookGraphRequest request, final GDXFacebookCallback<JsonResult> callback) {
    String accessToken = null;
    if (getAccessToken() != null) {
      accessToken = getAccessToken().getToken();
    }

    if (request.isUseCurrentAccessToken() && accessToken != null) {
      request.putField("access_token", accessToken);
    }

    Net.HttpRequest httpRequest = new Net.HttpRequest(request.getMethod());
    String url = request.getUrl() + config.GRAPH_API_VERSION + "/" + request.getNode();
    httpRequest.setUrl(url);
    httpRequest.setContent(request.getContentAsString());
    httpRequest.setTimeOut(request.getTimeout());

    HttpRequestBuilder builder = new HttpRequestBuilder().newRequest();
    builder.method(request.getMethod());

    Gdx.net.sendHttpRequest(
        httpRequest,
        new Net.HttpResponseListener() {

          @Override
          public void handleHttpResponse(Net.HttpResponse httpResponse) {
            String resultString = httpResponse.getResultAsString();
            int statusCode = httpResponse.getStatus().getStatusCode();

            if (statusCode == -1) {
              GDXFacebookError error =
                  new GDXFacebookError(
                      "Connection time out. Consider increasing timeout value by using setTimeout()");
              callback.onError(error);
            } else if (statusCode >= 200 && statusCode < 300) {
              callback.onSuccess(new JsonResult(resultString));
            } else {
              GDXFacebookError error = new GDXFacebookError("Error: " + resultString);
              callback.onError(error);
            }
          }

          @Override
          public void failed(Throwable t) {
            t.printStackTrace();
            callback.onFail(t);
          }

          @Override
          public void cancelled() {
            callback.onCancel();
          }
        });
  }
コード例 #2
0
  protected void startSilentSignIn() {
    if (accessToken != null) {
      Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Starting silent sign in.");
      GDXFacebookGraphRequest request = new GDXFacebookGraphRequest();
      request.setMethod(Net.HttpMethods.POST);
      request.setNode("");
      request.putField(
          "batch",
          "[{\"method\":\"GET\", \"relative_url\":\"me\"},{\"method\":\"GET\", \"relative_url\":\"me/permissions\"}]");
      request.putField("include_headers", "false");
      request.useCurrentAccessToken();
      newGraphRequest(
          request,
          new GDXFacebookCallback<JsonResult>() {

            @Override
            public void onSuccess(JsonResult result) {
              JsonValue value = result.getJsonValue();
              if (value != null && value.isArray()) {

                JsonValue meValue = value.get(0);
                JsonValue permissionsValue = value.get(1);

                if (jsonHasCode200AndBody(meValue) && jsonHasCode200AndBody(permissionsValue)) {

                  JsonReader reader = new JsonReader();
                  JsonValue permissionBodyValue = reader.parse(permissionsValue.getString("body"));
                  JsonValue permissionArray = permissionBodyValue.get("data");

                  Array<String> grantedPermissions = new Array<String>();
                  for (int i = 0; i < permissionArray.size; i++) {
                    JsonValue permission = permissionArray.get(i);

                    if (permission.getString("status").equals("granted")) {
                      grantedPermissions.add(permission.getString("permission").toLowerCase());
                    }
                  }

                  if (arePermissionsGranted(grantedPermissions)) {
                    Gdx.app.debug(
                        GDXFacebookVars.LOG_TAG,
                        "Silent sign in successful. Current token is still valid.");
                    callback.onSuccess(
                        new SignInResult(
                            accessToken,
                            "Silent sign in successful. Current token is still valid."));
                  } else {
                    signOut();
                    Gdx.app.debug(
                        GDXFacebookVars.LOG_TAG,
                        "Used access_token is valid but new permissions need to be granted. Need GUI sign in.");
                    callback.onError(
                        new GDXFacebookError(
                            "Used access_token is valid but new permissions need to be granted. Need GUI sign in."));
                    startGUISignIn();
                  }

                } else {
                  signOut();
                  Gdx.app.debug(
                      GDXFacebookVars.LOG_TAG, "Silent sign in error: " + value.toString());
                  callback.onError(new GDXFacebookError(value.toString()));
                  startGUISignIn();
                }
              } else {
                callback.onError(
                    new GDXFacebookError(
                        "Unexpected error occurred while trying to sign in. Error unknown, possible timeout."));
              }
            }

            private boolean arePermissionsGranted(Array<String> grantedPermissions) {
              for (int i = 0; i < permissions.size; i++) {
                if (!grantedPermissions.contains(permissions.get(i).toLowerCase(), false)) {
                  return false;
                }
              }
              return true;
            }

            @Override
            public void onError(GDXFacebookError error) {
              signOut();
              Gdx.app.debug(
                  GDXFacebookVars.LOG_TAG, "Silent sign in error: " + error.getErrorMessage());
              callback.onError(error);
              startGUISignIn();
            }

            @Override
            public void onFail(Throwable t) {
              signOut();
              Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in failed: " + t);
              callback.onFail(t);
              startGUISignIn();
            }

            @Override
            public void onCancel() {
              signOut();
              Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in cancelled");
              callback.onCancel();
              startGUISignIn();
            }
          });
    } else {
      Gdx.app.debug(GDXFacebookVars.LOG_TAG, "Silent sign in cancelled. No existing access token.");
    }
  };