导入Windows Live联系人

时间:2014-09-18 09:00:25

标签: php msdn windows-live

我开始从直播导入联系人。现在我不知道MS在想什么,但他们认真地把他们所掌握的一切都复杂化了。

对于我的应用,我获得一个电话号码非常重要。事实上,如果你没有电话号码那么重要,你的联系人就会被忽略。用我的方法我看不到任何电话号码。我假设如果我逐个遍历每个联系人,就会显示出来,但是唉,没有爱。

这是我的方法:

$import_id = time();
$client_id = "xxx";
$redirect_uri = 'redirecturi';
$client_secret = "xxx";
$code = $_GET['code'];
$grant_type = "authorization_code";

$post = "client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code&grant_type=$grant_type";
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"https://login.live.com/oauth20_token.srf");
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($curl);
curl_close($curl);
$token = json_decode($result);
$access_token = $token->access_token;
$user_id = $token->user_id;

$url = "https://apis.live.net/v5.0/me/contacts?access_token=$access_token";
$response =  curl_file_get_contents($url);
$response = json_decode($response);
foreach($response->data as $contact) {
    $contact_details = curl_file_get_contents("https://apis.live.net/v5.0/" . $contact->id . "?access_token=$access_token");
    debug($contact_details);
}
die();

但是,我只是得到这样的信息(这个我认识的人有一个联系电话,因为我在people.live.com上查看他时可以看到它):

{
   "id": "contact.id", 
   "first_name": "Danie", 
   "last_name": "Van den Heever", 
   "name": "Danie Van den Heever", 
   "is_friend": false, 
   "is_favorite": false, 
   "user_id": "userid", 
   "email_hashes": [
      "emailhash"
   ], 
   "updated_time": "2014-09-17T12:11:10+0000"
}

我的权限请求url(定义范围)如下所示:

https://login.live.com/oauth20_authorize.srf?client_id=clientidkey&scope=wl.basic%20wl.offline_access&response_type=code&redirect_uri=redirecturi

我应该添加更多范围来获取联系号码吗?如果是这样,哪个范围?或者这不可能吗?

2 个答案:

答案 0 :(得分:4)

解决方案是使用未记录的范围wl.contacts_phone_numbers,存在被弃用或仅被锁定的风险,只有Microsoft批准的客户端才能使用它,但同时它可以使用

此外,您无需为每个联系人执行额外请求,您从me/contacts获得的联系对象已在phones对象中包含电话号码。

顺便说一下,这是我在测试时使用的代码,我使用了REST client library,它避免了每次复制/粘贴长且重复的cURL参数,并将请求转换为单行。

要求许可的代码:

$params = ["client_id" => "...", "scope" => "wl.basic wl.contacts_phone_numbers", "response_type" => "code", "redirect_uri" => "http://sanctuary/contacts_callback.php"];

header("Location: https://login.live.com/oauth20_authorize.srf?".http_build_query($params));

请注意权限请求中的额外wl.contacts_phone_numbers范围。

获取访问令牌和检索联系人的代码:

// Composer's autoloader, required to load libraries installed with it
// in this case it's the REST client
require "vendor/autoload.php";

// exchange the temporary token for a reusable access token
$resp = GuzzleHttp\post("https://login.live.com/oauth20_token.srf", ["body" => ["client_id" => "...", "client_secret" => "...", "code" => $_GET["code"], "redirect_uri" => "http://sanctuary/contacts_callback.php", "grant_type" => "authorization_code"]])->json();
$token = $resp["access_token"];

// REST client object that will send the access token by default
// avoids writing the absolute URL and the token each time
$client = new GuzzleHttp\Client(["base_url" => "https://apis.live.net/v5.0/", "defaults" => ["query" => ["access_token" => $token]]]);

// get all the user's contacts
$contacts = $client->get("me/contacts")->json()["data"];

// iterate over contacts
foreach ($contacts as $contact) {
    // if that contact has a phone number object
    if (array_key_exists("phones", $contact)) {
        // iterate over each phone number
        foreach ($contact["phones"] as $phone) {
            // if number isn't blank
            if (!empty($phone)) {
                // do whatever you want with that number
            }
        }
    }
}

这是me/contacts与额外范围相似的内容(减去几个换行符和个人信息):

Array (
    [data] => Array (
            [0] => Array (
                    [id] => contact...
                    [first_name] => ...
                    [last_name] => ...
                    [name] => ...
                    [is_friend] => 
                    [is_favorite] => 
                    [user_id] => 
                    [email_hashes] => ...
                    [updated_time] => ...
                    [phones] => Array ( // what you asked for
                            [personal] => 
                            [business] => 
                            [mobile] => +337...
                        )
                )
        )
)

答案 1 :(得分:0)

通过阅读the documentation,电话号码是User object的一部分。

要获取他们的电话号码,你会;

  • 抓住他们的联系人列表(您已经完成)
  • 遍历结果集
  • 从联系人回复中抓取用户ID。 (键id
  • User集合(wl.phone_numbers范围)
  • 发出请求
  • 查看电话号码是否为空
    • 如果它们为NULL,则跳过迭代

示例手机对象(在User响应中);

"phones": {
   "personal": "(555) 555-1212", 
   "business": "(555) 111-1212", 
   "mobile": null
} 

因此;

$arrUser = json_decode($strResponse, true);

if( is_null($arrUser['phones']['personal']) 
     AND is_null($arrUser['phones']['business']
      AND is_null($arrUser['phones']['mobile']) ) {
   //No phone numbers
   //Assuming you're in a loop, fetching a user object for each contact - skip the iteration & move onto the next contact.
   continue;
}