Cargue el archivo en Google Drive usando el token de acceso. ¿Qué alcance de autenticación se requiere?

I am trying to upload a file to Google Drive with PHP.

For the authorization code, I am using the following scopes:

https://www.googleapis.com/auth/drive  
https://docs.google.com/feeds/  
https://docs.googleusercontent.com/  
https://spreadsheets.google.com/feeds/  

I already received a valid refresh_token, and I'm able to use it to fetch a new access_token anytime I wish.

Now the problem is when I am trying to perform the upload using this access_token (it is not exprired, and not used yet) dice:

stdClass Object
(
    [error] => stdClass Object
        (
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [domain] => global
                            [reason] => required
                            [message] => Login Required
                            [locationType] => header
                            [location] => Authorization
                        )

                )

            [code] => 401
            [message] => Login Required
        )
)

I am using curl with following header:

Array
(
    [0] => Content-Type: text/plain
    [1] => Content-Length: 503242
    [2] => Authorization: Bearer ya29.AHES6ZRWXZkZDDi6AFV9PK2QonHp93nIfJbYqipXFT1uCsg
    [3] => GData-Version: 3.0
)

And following URL:

https://www.googleapis.com/upload/drive/v2/files?uploadType=media

The CURL Command is like:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADERS, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file));
$return = curl_exec($ch);
curl_close($ch);

Any Help will be heartly appreciated... Thanks in advance...

preguntado el 28 de agosto de 12 a las 14:08

3 Respuestas

The only OAuth scope you need to upload a file to Drive is https://www.googleapis.com/auth/drive.file.

For all possible scopes, check the documentation: https://developers.google.com/drive/scopes

By the way, the Drive API is not GData-based so you don't need to add the GData-Version encabezado a sus solicitudes.

I'd also recommend you to compare your requests with those sent by the OAuth 2.0 Playground so that it should be easy to spot the error:

https://developers.google.com/oauthplayground/

Respondido 28 ago 12, 18:08

Try using the following scope: https://www.googleapis.com/auth/drive.file

Also verify that you have gone through the steps given in documentation thoroughly

https://developers.google.com/drive/about-auth

If you don't want to use curl.You can use insert method of Googlephpclientlibarary of inserting files.

Otherwise compare request with OauthPlayground as mentioned by Claudio

Respondido 30 ago 12, 12:08

I was in very much hurry, and after much effort the whole night, I came to a point where I made a minor modification in the Google API Client Library... And guess what! It works!! I am off desk right now, Will post the changes later... Thanks, Claudio y Trim Kaur - Izhar Aazmi

CURLOPT_HTTPHEADER , not CURLOPT_HTTPHEADERS

Respondido 17 Oct 15, 19:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.