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

268 out of 411 rated this helpful - Rate this topic

Important  

If you are a Outlook.com customer who needs help with your Outlook.com account, go to Microsoft Answers—Outlook.com, Windows Live Messenger & Microsoft SkyDrive or to your Account overview.

This topic is for software developers and does not describe how to resolve problems with your Outlook.com account. Help requests entered in this topic's Did you find this helpful dialog box will not be answered.

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

 

 

Build date: 5/15/2013

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.