All mobile clients are treated as insecure. They require a separate secure backend to retrieve a specific client key, before they can be used with the YesGraph API. This is required to ensure it is not possible to retrieve contacts from other users to protect their privacy. So each user can only access their own contacts submitted to the API. The details are explained in the Authentication document.

Before you can use YesGraph API, you will need a secret key for your secure backend, so go ahead and register. Multiple examples of backend calls to YesGraph API are available below.

Using a Custom Python Server

We have an example Python Flask app that connects to YesGraph API available at GitHub. After you have deployed this server to Heroku, place your secret key in a Config Variable under the Settings tab.

Now that you have a secure server that can generate client-keys with the YesGraph API, you must call it from within your app. The Android YesGraph SDK comes with a fetchClientKeyForUserId method, which takes as input the apiUrl for the server you just setup, a userId, and a callback that sets the client-key for that user with configureWithClientKey.

// This is a sample call to get the client key:
String apiUrl = "http://yesgraph-client-key-test.herokuapp.com/";
MyClientKey myClientKey = new MyClientKey();
myClientKey.fetchClientKeyForUserId(apiUrl, userId, new Handler.Callback() {
  @Override
  public boolean handleMessage(Message msg) {

    JSONObject jsonObject = (JSONObject) msg.obj;
    try {
      String clientKey = jsonObject.getString("message");

      // Get the client key and set it. It is saved and used in further API calls.
      yesGraphApplication.configureWithClientKey(clientKey);
      return true;
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return false;
  }

});

Using Parse Cloud Code

To generate a YesGraph API client key using Parse, you will need a Parse backend application.

We provide a Parse Example with JavaScript Cloud Code to generate the key for the app.

The code below can be used on Parse Cloud Code generate a call to YesGraph API to retrieve a client key for specific user ID. It requires the YesGraph API secret key to use.

Parse.Cloud.define("YGgetClientKey", function(request, response) {
    var httpOptions = {
        url: "https://api.yesgraph.com/v0/client-key",
        method: "POST",
        headers: {  "Authorization": "<YESGRAPH_SECRET_KEY>",    // enter secret key for your YesGraph application
                    "Content-Type": "application/json"
        },
        body: {"user_id": request.params.userId}
    }

    Parse.Cloud.httpRequest(httpOptions).then(
        function(httpResponse) {
            response.success(httpResponse.text);
        },
        function(httpResponse) {
            console.log(httpResponse);
            var error = new Error();
            error.code = httpResponse.status;
            error.message = httpResponse.text;
            response.error(error);
        }
    );
});

Then call the function from your app:

YesGraph yesGraphApplication = (YesGraph) getApplicationContext();

String APPLICATION_ID = getString(R.string.parse_app_id);
String CLIENT_KEY = getString(R.string.parse_app_id);

Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);

yesGraphApplication.configureWithUserId("<USER_ID>");
yesGraphApplication.setSource("Name", "+1 123 123 123", "Email");

// Calling a function in Parse backend to get a client key
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("userId", "<USER_ID>");

ParseCloud.callFunctionInBackground("YGgetClientKey", params, new FunctionCallback<String>() {
  public void done(String clientKey, ParseException e) {
    //set client key to YesGraph SDK
    yesGraphApplication.configureWithClientKey(clientKey);
}
});

Custom Server Implementation

Alternatively you can always implement your own backend server to authenticate insecure clients. Detailed documentation is available at:

Authenticating YesGraph SDK

Once you have configured your server’s client-key endpoint, you must setup your Android app to send an HTTP request to that endpoint with your userId. It should return a client-key, which you will configure the YesGraph Android SDK with. This must be a unique UserID.

Every time a user logs in, you must call both configureWithUserId: and configureWithClientKey: methods. This ensures all YesGraph API calls are made for the correct user. Both values are persisted in the SDK, so you do not need to call them more than once manually, even if the application is closed.

private YesGraph yesGraphApplication;

yesGraphApplication = (YesGraph) getApplicationContext();
yesGraphApplication.configureWithUserId("<USER_ID>");
yesGraphApplication.configureWithClientKey("<CLIENT KEY>");

If your user is yet not logged in, you can generate an anonymous random user ID with the YesGraph Utility.randomUserId method in the Utility class.

String randomUserId = Utility.randomUserId();