perl - 使用 shuttlecloud 获取谷歌联系人

标签 perl http google-api google-oauth

我无法使用 shuttlecloud 获取 google 用户的联系人。我根据 shuttlecloud 和谷歌文档做了一切。我使用基于密码的身份验证(例如 mail.ru)获得了用户与 shuttlecloud 的联系。我创建 url 并将其放入浏览器 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 创建 token 并粘贴到脚本时 - 一切正常。由此,我没有正确创建访问 token ,但无法理解我做错了什么。

最佳答案

在文档中说:

This request returns the status of the job and the extracted contacts up to that point in time from the account.

The server provides a URL from which the real-time progress can be retrieved.

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

关于perl - 使用 shuttlecloud 获取谷歌联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30733845/

相关文章:

c#-4.0 - 如何使用 youtube v3 api 和 C# 从 youtube 删除视频

python - 使用 Mechanize 进行 Python 抓取脚本的 HTTP 错误 401

perl - 为什么 IPC::SysV->shmget 以 EINVAL 响应?

linux - Perl:如何在解压缩 tar.gz 文件时创建新目录

perl - 我可以在 Perl 的反引号中执行多行命令吗?

http - 浏览器如何检测哪个 HTTP 响应是他们的?

使用 Curl HTTP Post 到 Ebay 交易 API

google-api - 无法使用 youtube data api v3 获取 youtube channel 统计信息

php - 使用 Laravel Socialite 获取谷歌刷新 token

perl - 如何为Net::WebSocket::Server程序编写客户端程序?