Google Drive - Exception - Could not json decode the token

Re: Google Drive - Exception - Could not json decode the token

by Jorge Merino -
Number of replies: 12

Hi,

I know  this option is not the most clean, but in my case it works.

In the method makeRequest of the file lib/google/lib/curlio.php, I had to add

....

global $CFG;

.....

// Parse out the raw response into usable bits.        
list($responseHeaders, $responseBody) = self::parseHttpResponse($respdata, $respheadersize);


if (!empty($CFG->proxyhost)) {                    
       list($responseHeaders, $responseBody) = self::parseHttpResponse($responseBody,0);

....

 

I hope it is useful.

In reply to Jorge Merino

Re: Google Drive - Exception - Could not json decode the token

by Tim Rees -

Hi Guys,


I have the same issue with Moodle 2.7. I migrated last night, and set up the API again, and I still get the Exception - Could not json decode the token.


I did try that previously mentioned code adjustment, but that crashed the entire files.php


I am behind a proxy at the moment, but this has worked correctly on the previous server. I am working with my head office to get the firewall adjusted so I can have an open port, but this may take ages...


Any ideas?


Thanks!

In reply to Tim Rees

Re: Google Drive - Exception - Could not json decode the token

by Tim Rees -

Got my head office to open some firewall ports, and it works correctly. There is something wrong in the code, and it no longer uses the proxy properly.


In reply to Jorge Merino

Re: Google Drive - Exception - Could not json decode the token

by Zvonko Martinović -

I had the same problem as part of the header is not removed from response when server is behind proxy. As response is always json I just calculate that header is everything before first curly bracket.

So in /lin/google/curlio.php add following changes:

public function makeRequest(Google_HttpRequest $request) {
global $CFG;
...
// Parse out the raw response into usable bits.
if(!empty($CFG->proxyhost)) {     
	$respheadersize = strlen(strstr($respdata, '{', true))-1;
	list($responseHeaders, $responseBody) = self::parseHttpResponse($respdata, $respheadersize);
}
else{
	list($responseHeaders, $responseBody) = self::parseHttpResponse($respdata, $respheadersize);
}
Average of ratings: Useful (1)
In reply to Zvonko Martinović

Re: Google Drive - Exception - Could not json decode the token

by Zvonko Martinović -

Or to shorten it a bit:


if($CFG->proxyhost) { 
	$respheadersize = strlen(strstr($respdata, '{', true))-1;
}
list($responseHeaders, $responseBody) = self::parseHttpResponse($respdata, $respheadersize);
Average of ratings: Useful (1)
In reply to Zvonko Martinović

Re: Google Drive - Exception - Could not json decode the token

by R Ali -

Good day,


I have the same problem and I want to try this solution but it is not very clear where exactly in the /curlio.php file do I put the code. 


using Moodle 2.8 


Thank you.

In reply to R Ali

Re: Google Drive - Exception - Could not json decode the token

by Zvonko Martinović -

I see they have changed quite a bit this file, but still I guess you can try to add it:


public function executeRequest(Google_Http_Request $request) {
	global $CFG;
	
	$curl = new curl();
	if ($request->getPostBody()) {
		$curl->setopt(array('CURLOPT_POSTFIELDS' => $request->getPostBody()));
	}

...


	if($CFG->proxyhost) {
		$respheadersize = strlen(strstr($respdata, '{', true))-1;
	}


	list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize);

Haven't tried it but it should work.

In reply to Zvonko Martinović

Re: Google Drive - Exception - Could not json decode the token

by R Ali -

Hello Zvonko,

Thank you very much for your response.  The code does work for google portfolio because I was getting "error/moodle/Cantpostupload" initially but I can now easily upload to portolio without error. But not so with the repository. I can quite access the repository and see files in there but when I click on any file I get "Exception - Invalid json in service response:". If I try again to access the repository the error will be repeated without accessing the google drive repository.

Thank you for helping with this.

Rafiat



In reply to R Ali

Re: Google Drive - Exception - Could not json decode the token

by Zvonko Martinović -

Hello Rafiat,


sorry I don't have Moodle 2.8 installed so I cant test. But you can take a look at variable $responseBody after list($responseHeaders, $responseBody) = $this->parseHttpResponse($respdata, $respheadersize); and see if it's valid json.


Zvonko

In reply to Zvonko Martinović

Re: Google Drive - Exception - Could not json decode the token

by Yogesh Nahar -

Hi Zvonko,

Now my login seems to be going through fine but I get an invalid JSON in service response error. Please see attachment. I don't have any other debug info displayed. At one point of time I had my Drive files show up and I could even select them, but since then no luck.

I don't know how I ended up getting my Drive files to show up, but I made a small change to the .htaccess file where I put in the line: php_value auto_append_file none

Any thoughts on why this could be happening?

Thanks. 

--
Yogesh Nahar

Attachment error screenshot.png
In reply to Yogesh Nahar

Re: Google Drive - Exception - Could not json decode the token

by Javier Osset -

Hello,

I have the same problem, and recieve both messages: Could not json decode the token (first, when I sign in the google acount) & invalid json in service response  (then, when I try to open the repository two or three times more).

I have shared hosting with Blue Host and Moodle 2.7.7

I have Moodle and Google drive with my students and I would like that they can take a document shared by a little group from Drive and save it in the Moodle Course.

I tried the Zvonko solution but it still no work.

I think I have no proxy in my personal internet conexion.

Thanks a lot
In reply to Zvonko Martinović

Re: Google Drive - Exception - Could not json decode the token

by Yogesh Nahar -

Hi Zvonko,

I tried your fix but am still unable to make this work. I am running Moodle v2.6.1.

This is the debug message I am getting:

--

Exception - Could not json decode the token

More information about this error

Debug info: 

Error code: generalexceptionmessage

Stack trace:

line 153 of /lib/google/auth/Google_OAuth2.php: Google_AuthException thrown

line 104 of /lib/google/auth/Google_OAuth2.php: call to Google_OAuth2->setAccessToken()

line 131 of /lib/google/Google_Client.php: call to Google_OAuth2->authenticate()

line 121 of /repository/googledocs/lib.php: call to Google_Client->authenticate()

line 62 of /repository/repository_callback.php: call to repository_googledocs->callback()

--

Please advice if you can help here. I have noticed that a no. of people have had this issue but there isn't any fix I can find in the forums. 

Thank you.

--
Yogesh Nahar

In reply to Yogesh Nahar

Re: Google Drive - Exception - Could not json decode the token

by Yogesh Nahar -

I had to finally give up on this and use Dropbox. It worked flawlessly. 

Yogesh