使用shuttlecloud获取谷歌联系人

时间:2015-06-09 13:45:09

标签: perl http google-api google-oauth2

我无法使用shuttlecloud获取Google用户的联系人。我根据shuttlecloud和谷歌文档做了一切。我使用基于密码的身份验证(例如mail.ru)获得了用户与shuttlecloud的联系。我创建了网址并将其添加到浏览器https://accounts.google.com/o/oauth2/auth?redirect_uri=http://localhost:80&response_type=code&client_id=my_id.apps.googleusercontent.com&scope=https://www.googleapis.com/auth/contacts.readonly&access_type=offline

我获取代码并复制到脚本:

my $url = 'https://demo.api.shuttlecloud.com/beta/load';
my $auth_url = 'https://demo.api.shuttlecloud.com/beta/auth/capabilities';
my $appid = 'my_shuttlecloud_id';
my $ts = int(time);
my $secret = 'my_shuttlecloud_secret';
my $sig = sha256_hex("{".$appid.":".$secret.":".$ts."}");
my $url_tok = 'https://www.googleapis.com/oauth2/v3/token';
my $code = '4/GmVQi8AgNzajSo0mTudUBumjHyHhhb9EBxN8ufG6z34.QjE7Q6MjmUweWmFiZwPfH01FreiwmwI';
my $id = 'my_google_id';
my $sec = 'my_google_secret';
my $redirect = 'http://localhost:80';
my $grant_type = 'authorization_code';

my $response = $ua->request(POST $url_tok,
       Content_Type => 'application/x-www-form-urlencoded',
       Content      => [ code  => $code,
                         redirect_uri => $redirect,
                         client_id => $id,
                         client_secret => $sec,
                         grant_type   => $grant_type,
                       ]);
my $cod = $response->content;
my $decoded_json = decode_json( $cod );
my $token = @{$decoded_json}{'access_token'};
my $email = 'my_google_email@gmail.com';
my $data = '{
    "contactsextraction": {
        "sourceaccount": {
            "userid": {
                        "email": "'.$email.'"
            },
            "auth": {
            "3loauth": "'.$token.'"
            }
        }
    }
}';

$url = $url."?appid=".$appid."&ts=".$ts."&sig=".$sig;
my $req = HTTP::Request->new(POST => $url);
$req->content($data);
my $resp = $ua->request($req);
my $message;
if ($resp->is_success) {
    $message = $resp->decoded_content;
}
else {
    print "error code: ", $resp->code, "\n";
    print "error message: ", $resp->message, "\n";
}
$decoded_json = decode_json( $message );
my $ft_url = @{$decoded_json}{'fetchurl'};
$ft_url = $ft_url."?appid=".$appid."&ts=".$ts."&sig=".$sig;
sleep 5;
$req = HTTP::Request->new(GET => $ft_url);
$resp = $ua->request($req);
if ($resp->is_success) {
    $message = $resp->decoded_content;
}
else {
    print "error code: ", $resp->code, "\n";
    print "error message: ", $resp->message, "\n";
}
$decoded_json = decode_json( $message );
print Dumper ($decoded_json);

我从shuttlecloud json收到:'status' => 'STARTED'没有联系人。 当我使用OAuth 2.0 Playground创建令牌并粘贴到脚本时 - 一切正常。接下来,我没有正确创建访问令牌,但无法理解我做错了什么。

2 个答案:

答案 0 :(得分:1)

在文档中说:

  

此请求返回作业和提取的联系人的状态   到帐户的那个时间点。

     

服务器提供实时进度的URL   检索。

您需要进行轮询以检索联系人,直到他们准备好。它非常快,通常是几秒钟,但不是即时的。尝试在等待1秒的循环中执行联系人获取请求,例如在每个请求之间,您应该获得所有联系人。

答案 1 :(得分:0)

答案是荒谬和愚蠢的。我不知道必须在Google控制台中激活API。您可以看到如何执行此操作here