YesGraph iOS SDK is treated as unsecure client. It requires a separate secure backend to retrieve a specific client key, before it can be used with 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 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.

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:

PFCloud.callFunctionInBackground("YGgetClientKey", withParameters: parameters, block: { response, error in
    if let response = response?.dataUsingEncoding(NSUTF8StringEncoding) {
        do {
            let jsonObject = try NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            
            if let YSGclientKey : String = jsonObject["client_key"] as? String {
                YesGraph.shared().configureWithClientKey(YSGclientKey)
                
                if let completion = completion {
                    completion (success: true, error: nil)
                }
            }
        }
        catch {
            
        }
    }
    else if let completion = completion, error = error {
        completion (success: false, error: error)
    }
})
// Calling a function in Parse backend to get a Client key
[PFCloud callFunctionInBackground:@"YGgetClientKey"
                   withParameters:parameters
                            block:^(NSString *response, NSError *error)
{
    if (!error)
    {
        NSData *responseData = [response dataUsingEncoding:NSUTF8StringEncoding];
        
        NSError *jsonSerializationError;
        
        id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:(NSJSONReadingMutableContainers) error:&jsonSerializationError];
        NSString *YSGclientKey = [jsonObject objectForKey:@"client_key"];
        
        if (jsonSerializationError || !YSGclientKey)
        {
            NSLog(@"Json serizalization error: %@", jsonSerializationError.description);
            
            if (completion)
            {
                completion(NO, jsonSerializationError);
            }
        }
        else
        {
            [[YesGraph shared] configureWithClientKey:YSGclientKey];
            
            if (completion)
            {
                completion(YES, nil);
            }
        }
    }
    else
    {
        if (completion)
        {
            completion(NO, error);
        }
    }
}];

Custom Server Implementation

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

Authenticating YesGraph SDK

Once you finish configuring, you should set up the YesGraph SDK with the User ID for the user who is on the service. This must be a unique User ID.

YesGraph.shared().configureWithUserId("<USER_ID>")
YesGraph.shared().configureWithClientKey("<CLIENT_KEY>")
[[YesGraph shared] configureWithUserId:@"<USER_ID>"];
[[YesGraph shared] configureWithClientKey:@"<CLIENT_KEY>"];

If your user is yet not logged in, you can generate an anonymous random user ID with randomID method in YSGUtility class.

let randomUserID = YSGUtility.randomID()
NSString *randomUserId = [YSGUtility randomID];

Every time a user logs in, you must call both configureWithUserId: and configureWithClientKey:. 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.