2

我已经开始从现场导入联系人。现在我不知道 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

我应该添加更多范围以获取联系电话吗?如果是这样,哪些范围?或者这是不可能的?

4

2 回答 2

4

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

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

顺便说一句,这是我在测试时使用的代码,我使用了一个REST 客户端库,它避免了每次复制/粘贴冗长且重复的 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...
                        )
                )
        )
)
于 2014-10-07T13:45:26.447 回答
0

通过阅读文档,电话号码是User对象的一部分。

要获取他们的电话号码,您可以;

  • 获取他们的联系人列表(您已完成)
  • 遍历结果集
  • 从联系人响应中获取用户 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;
}
于 2014-10-07T11:00:16.183 回答