正在导入Windows Live联系人



我已经开始从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

我应该添加更多的作用域来获取联系人号码吗?如果是,哪些作用域?或者这是不可能的?

解决方案是使用未记录的作用域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 = GuzzleHttppost("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 GuzzleHttpClient(["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...
                        )
                )
        )
)

通过阅读文档,电话号码是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;
}

相关内容

  • 没有找到相关文章

最新更新