Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Find people in a user's contact list

Find people in a user's contact list that are registered with your site

Recover your account here

Important  

The Live Connect APIs are deprecated. We recommend that you use the Outlook REST APIs to build your solutions. This will extend your reach to Outlook.com users and Office 365 enterprise customers. Although the Live Connect APIs will be supported in Outlook.com for the short term, existing Live Connect API solutions might stop working without advanced notice. If your app is using IMAP with OAuth 2.0, it will continue to work, but our REST APIs are the primary APIs for building apps that connect to Outlook.com and Office 365. Read the article on how you can take advantage of the Outlook REST APIs.

This topic describes how your website can determine whether any contacts of a visiting user are also registered users of your website.

When a user with a Microsoft account visits your website, you can determine whether any contacts of the visitor are also registered users of your website. You do this by using a combination of client-side JavaScript code and server-side code.

To tell whether any of a user's contact are registered with your site, do this:

After you have the list of hashes that represent your registered users' email addresses, and the list of hashes from the e-mail addresses in the user's contacts, you can compare them to find the matches.

To get the full benefit of the JavaScript, C#, Objective-C, and Java code examples in this topic, you can use them in the context of larger code reference samples that we provide in Working with the code examples.

Generate a set of email-address hashes

Generate a hash that corresponds to the email address for each of your website's registered users. You must generate each hash by following these steps:

  1. Trim leading and trailing white spaces from the email address; for example, Someone@Example.org.
  2. Trim leading and trailing white spaces from the requesting app's client ID; for example, 0000000603DB0F.
  3. Concatenate the results of steps 1 and 2; for example, Someone@Example.org0000000603DB0F.
  4. Convert the result of step 3 to lowercase; for example, someone@example.org0000000603db0f.
  5. Convert the result of step 4 to binary using UTF8 encoding.
  6. Calculate the SHA-256 value of step 5 and convert the value to its hexadecimal equivalent, for example, 6A9F...63A4. (Part of the value is omitted here for brevity.)
  7. Convert the results of step 6 to lowercase, for example, 6a9f...63a4. The result of this step is the email-address hash.

Top

Get a list of email-address hashes with client-side code

You can get a list of email addresses for a user's contacts by using code like this.

Get a list of email-address hashes in JavaScript


function friendFinder_onClick() {
    WL.login({
        scope: "wl.basic"
    }).then(
        function (response) {
            WL.api({
                path: "me/contacts",
                method: "GET"
            }).then(
                function (response) {
                    var resultData = response.data;
                    var emailHashes = new Array;
                    for (i = 0; i < resultData.length; i++) {
                        for (j = 0; j < resultData[i].email_hashes.length; j++) {
                            emailHashes[emailHashes.length] = resultData[i].email_hashes[j];
                        }
                    }
                    var resultText = "";
                    for (k = 0; k < emailHashes.length; k++) {
                        resultText += emailHashes[k] + "\r\n";
                    }
                    document.getElementById("infoArea").setAttribute("rows", emailHashes.length);
                    document.getElementById("infoArea").value = resultText;
                },
                function (responseFailed) {
                    document.getElementById("infoArea").value =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}


Get a list of email-address hashes in C# for Windows Store apps and Windows Phone apps


private async void btnGetEmailHashes_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult = await liveClient.GetAsync("me");
        dynamic result = operationResult.Result;
        string message = "";
        List<object> data = null;
        IDictionary<string, object> contact = null;
        List<object> emailHashes = null;
        if (result.ContainsKey("data"))
        {
            data = (List<object>)result["data"];
            for (int i = 0; i < data.Count; i++)
            {
                contact = (IDictionary<string, object>)data[i];
                if (contact.ContainsKey("email_hashes"))
                {
                    emailHashes = (List<object>)contact["email_hashes"];
                    for (int j = 0; j < emailHashes.Count; j++)
                    {
                        message += emailHashes[j].ToString() + "\r\n";
                    }
                }
            }
        }
        infoTextBlock.Text = message;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting email hashes: " + exception.Message;
    }
}


[]

Get a list of email-address hashes in Objective-C for iOS


- (void) getEmailHashes
{
    [self.liveClient getWithPath:@"me/contacts" 
                        delegate:self];
}

- (void) liveOperationSucceeded:(NSDictionary *)result
{
    NSArray *contacts = [result objectForKey:@"data"];
    for (NSDictionary *contact in contacts) 
    {
        NSArray *emailHashes = [contact objectForKey:@"email_hashes"];
        for (NSString *emailHash in emailHashes)
        {
            self.displayLabel.text = [NSString stringWithFormat:@"%@,\n %@", self.displayLabel.text, emailHash];
        }
    }
}


Get a list of email-address hashes in Java for Android


public void friendFinder() {
    final LiveOperationListener opListener = new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText(exception.getMessage());
           }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
               try {
                   JSONArray contacts = result.optJSONArray("data");
                   List<String> emailHashes = new ArrayList<String>();
                   for (int i = 0; i < contacts.length(); i++) {
                      JSONObject contact = contacts.getJSONObject(i);
                      JSONArray contactEmailHashes = contact.getJSONArray("email_hashes");
                      if (contactEmailHashes != null) {
                          for (int j = 0; j < contactEmailHashes.length(); j++) {
                              emailHashes.add(contactEmailHashes.optString(j));
                          }
                      }
                  }
                  String resultText = "Email hashes:\n";
                  for (int k = 0; k < emailHashes.size(); k++) {
                      resultText += emailHashes.get(k) + "\n";
                  }
                  resultTextView.setText(resultText);
               }
               catch(JSONException ex) {
                   resultTextView.setText("Error getting email hashes: " + ex.getMessage());
               }
        }
       };
    auth.login(this, Arrays.asList(new String[] { "wl.basic" }), 
        new LiveAuthListener() {
               public void onAuthError(LiveAuthException exception, Object userState) {
                   resultTextView.setText("Error signing in: " + exception.getMessage());
               }
            public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
                client.getAsync("me/contacts", opListener);                    
               }
        }
    );
}


Top

Get a list of email-address hashes with server-side code

If a user signs in and consents to a wl.basic scope request, and you have an access token that corresponds to this request, you can retrieve a list of the user's contacts. For each email address that is available for a given contact, an email-address hash is displayed. You can extract the email-address hash by using your preferred coding language and platform. To get the list of email-address hashes, make a server-side HTTP GET call with REST and the following request parameters.

https://apis.live.net/v5.0/me/contacts?access_token=ACCESS_TOKEN

In the preceding REST code example, replace ACCESS_TOKEN with your access token string.

The JavaScript Object Notation (JSON)-formatted response collection will look similar to the following. (Ellipses appear in place of information that is omitted for brevity.)

{
   "data": [
      {
         "id": "contact.b4466224b2ca42798c3d4ea90c75aa56", 
         "first_name": "Henrik", 
         "last_name": "Jensen", 
         ...
         "email_hashes": [
            "9ecdb19f4eb8e04304c5d1280368c42e85b6e4fe39f08b0c837ec592b905a620", 
            "fc05492f50da6488aa14dcf221d395bcb29a4e43b43b250d60c68df4f831cad3"
         ], 
         ...
      }, {
         ...
      }
   ]
}

For more information about how to make server-side HTTP calls with REST, see Server-side scenarios.

Top

 

 

Show:
© 2015 Microsoft