Reputation: 11
I need a preview of my google drive files,are my archives,but are private,but I dont want to login in My account to preview the file,how I can use the Oauth Authentication to preview the files?if yes how i can do it.I try to generate the webContentLink but needs a login too,Thanks
$url_array = explode('?', 'My url permited');
$url = $url_array[0];
$client = new Google_Client();
$client->setClientId('My client ID');
$client->setClientSecret('My client secret');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');
$client->setState('offline');
$client->refreshToken('My refresh token with Oauth playground');
$service = new Google_Service_Drive($client);
and After the connection with API I do the upload to google drive
$content = file_get_contents($_FILES["file"]["tmp_name"]);//pega o conteudo do arquivo
$file = $service->files->create($fileMetadata, array(//função para inserir aonde tem 2 parametros,metadados e conteudo/tipo e etc do arquivo
'data' => $content,
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart',
'fields' => 'id,webViewLink,webContentLink,thumbnailLink'));
printf("File ID: %s\n", $file->id);
I need this webViewLink to put in Iframe but without request login when I enter in the webViewLink
Upvotes: 1
Views: 1308
Reputation: 117196
Lets consider private user data. Data that is owned by a user for example files you upload to your google drive account. The only one who can see those files is the users who have access to them. I cant just go over to your drive account and access your personal data.
Oauth2 gives us a way to grant applications (Clients) access to our personal data. When you want to use an application you login to your google account, and are prompted with a consent screen to grant the application access to your data. If the application (Client) requested offline access they will be given a refresh token which they can use at anytime to request an access token to access your data. In this way the application authenticates itself to google as you. Without the refresh token your code would be prompting the user to request permission to access their data, with it the application doesn't need to prompt again as you have already granted it.
With the access token the application will be able to access the google drive api and your stored data. This has nothing to do with the google drive web application.
accessing private user data
webViewLink will only work if the user in question who has the weblink has been granted access to the file or if the file is public. They will be prompted to login if they are not already logged in so just make sure you have sure the files with the users.
Remember Oauth is used to grant your application access to a users data. You access that data though an api. You cant programmaticly preview files using unless you create your own application for previewing files. Your not going to be able to preview files in the Google Drive Web Application. Users will need to login to the application with their own accounts to see files on the google drive web application.
Current logged in user
Please run the following statement it will show you the user that you are currently logged in as.
$service->about->GetAbout(array('fields' => '*'));
You cant access google drive without being logged in you are logged in using your refresh token.
Upvotes: 1