-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveBillsToDrive.php
More file actions
123 lines (101 loc) · 3.56 KB
/
saveBillsToDrive.php
File metadata and controls
123 lines (101 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require 'vendor/autoload.php';
define('APPLICATION_NAME', 'Gmail API Quickstart');
define('CREDENTIALS_PATH', '.credentials/gmail-api-quickstart.json');
define('CLIENT_SECRET_PATH', 'config/client_secret_app.json');
define('SCOPES', implode(' ', array(
Google_Service_Gmail::GMAIL_READONLY)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
var_dump(file_exists($credentialsPath), $credentialsPath);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
// Print the labels in the user's account.
$user = 'me';
/*
$results = $service->users_labels->listUsersLabels($user);
if (count($results->getLabels()) == 0) {
print "No labels found.\n";
} else {
print "Labels:\n";
foreach ($results->getLabels() as $label) {
printf("- %s\n", $label->getName());
}
}
*/
$options = Array (
"q" => "label:facturas newer:2015/05/01"
);
$results = $service->users_messages->listUsersMessages($user,$options);
if (count($results->getMessages()) == 0) {
print "No labels found.\n";
} else {
print "Messsages:\n";
foreach ($results->getMessages() as $message) {
$mid = $message->getId();
printf("- %s ", $mid);
$attachments = $service->users_messages->get($user,$mid)->getPayload()->getParts();
foreach ($attachments as $attachment) {
printf("\n-- %s %s ", $attachment->getFilename(),$attachment->getMimeType());
$re = "/^.*\\.xml$/";
if(preg_match($re, $attachment->getFilename(), $matches)){
printf("go ahead");
$aid = $attachment->getBody()->getAttachmentId();
$xml = $service->users_messages_attachments->get($user,$mid,$aid)->getData();
file_put_contents($attachment->getFilename(), $xml);
}
}
printf("\n");
}
}