getting google contacts using shuttlecloud

167 views Asked by At

I can not get the google user's contacts using shuttlecloud. I did everything according to the shuttlecloud and google documentation. I got a user's contact with shuttlecloud using password-based authentication(mail.ru for example). I create url and put it to browser 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

I get the code and copy to script:

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 = '[email protected]';
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);

I receive from shuttlecloud json: 'status' => 'STARTED' without contacts. When i create token using OAuth 2.0 Playground and paste to script - all works. Following from this, i do not correctly create access token, but can not understand what I'm doing wrong.

2

There are 2 answers

0
Kostiuk Aleksandr On BEST ANSWER

The answer is ridiculous and stupid. I did not know that the API must be activated in the Google console. You can see how to do this here.

2
Javier On

in the documentation it says:

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.

You need to do polling to retrieve the contacts until they are ready. It is quite fast, usually some seconds, but not instant. Try to do the contacts fetch request in a loop waiting 1 second for example between each request and you should obtain all the contacts.