-
Notifications
You must be signed in to change notification settings - Fork 1
/
tweetsUploader.php
55 lines (48 loc) · 1.73 KB
/
tweetsUploader.php
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
<?php
/**
* @version 1.0
* @package Twitter Challenge
* @category PHP
* @author Suraj kumar Singh <[email protected]>
* @since 24-08-2018
* @link https://surajkrsingh.000webhostapp.com/Twitter/
*
* Here prepare the download file for tweets in CVS to be upload in drive.
*
**/
session_start();
include_once 'lib/google_lib/Google_Client.php';
include_once 'lib/google_lib/contrib/Google_Oauth2Service.php';
require_once 'lib/google_lib/contrib/Google_DriveService.php';
$client = new Google_Client();
$client->setClientId('PUT_GOOGLE_CLIENT_ID');
$client->setClientSecret('PUT_GOOGLE_CLIENT_SECRET');
$client->setRedirectUri('PUT_GOOGLE_REDIRECT_URL');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
if (isset($_GET['code']) || (isset($_SESSION['access_token']))) {
$service = new Google_DriveService($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
} else
$client->setAccessToken($_SESSION['access_token']);
//prepare a csv file for drive
$fileName=$_SESSION['fileName'];
$path = 'assets/tmp_data/'.$fileName;
$file = new Google_DriveFile();
$file->setTitle($fileName);
$file->setMimeType('application/vnd.google-apps.spreadsheet');
$file->setDescription('Uploading the all tweets of user');
$createdFile = $service->files->insert($file, array(
'data' => file_get_contents($path),
'mimeType' => 'text/csv',
'uploadType' => 'multipart',
'fields' => 'id'));
unlink($path);
header('location:https://surajkrsingh.000webhostapp.com/Twitter/home.php?fileName=user');
} else {
$authUrl = $client->createAuthUrl();
header('Location: ' . $authUrl);
exit();
}
?>