Google Drive - Exception - Could not json decode the token

Google Drive - Exception - Could not json decode the token

by William Machin -
Number of replies: 30

Hi, I was hoping you guys could help me out.

I recently built a Moodle 2.5 site but I can't get our Google Drive repository working.

I have followed the instructions from this page - http://docs.moodle.org/25/en/Google_OAuth_2.0_setup - to the letter (including enabling the Drive API). I have also copied the client ID and secret into the moodle settings for google drive.

When I click on "Login" I'm asked for access to my Google drive account (and can see the site branding I set up on google api console), which I allow. It then redirects me back to our moodle site with the error:

Exception - Could not json decode the token

I have attached a screenshot below.

This is hosted on a Windows 2008R2 server on IIS7.5

I have two moodle sites on this server and they are both doing the same thing.

Any help would be hugely appreciated as we migrating our file store to google drive and really need to be able to access it from moodle.

Cheers,
Will

Attachment drivererror.JPG
Average of ratings: -
In reply to William Machin

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

by William Machin -
Can anyone shed any light on what might be causing this problem? I have just set up another moodle site on a Windows 2003 IIS6 server and I'm getting the same issue - even with a different google domain. I have a feeling it might be something to do with us using a proxy server. Unfortunately, there is no way to bypass this. I can whitelist domains and I already have for google.com and googleapis.com are there anymore that need to be done? Help!!
In reply to William Machin

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

by Dan Poltawski -

Hi William,

I'm not entirely sure what this problem is, but proxy does sound likely. Have you got the proxy configured in your Moodle settings?

thanks

Dan

In reply to Dan Poltawski

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

by William Machin -

Hi Dan,

Yes I've configured the proxy in Moodle. We use a smoothwall proxy server and have set up moodle to use a totally unfiltered and unmonitored connection through it. Still no joy though.

RSS feeds are working, FWIW.

My only way of checking this is to take the server home and try it on my Virgin Media connection. Moodle would be running locally only though and Google wouldn't be able to talk to the URL as the public IP would no longer match the DNS records. Would that cause any issues?

In reply to William Machin

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

by Matt Grinsell -

Any updates on this?  Having same issue.  Exact same stack.

In reply to Matt Grinsell

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

by William Machin -

Matt,

We are no further along trying to resolve the issue. Can I ask, are you sat behind a proxy server and are you able to bypass it?

Cheers,
Will

In reply to Dan Poltawski

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

by Kevin Stewart -

I'm also having the same problem, same stack.  I'm not using any proxy however.

In reply to Kevin Stewart

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

by Shaun Daubney -

It's fine with Picasa using the same Google API Client ID and Secret.

In reply to William Machin

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

by Joel Roberts -

I'm also having the same problem on Moodle 2.5. Anybody have any ideas?

In reply to Joel Roberts

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

by William Machin -

Joel, are you using a proxy server to connect to the internet from your Moodle server?

In reply to William Machin

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

by Joel Roberts -

Yes, but I tried to eliminate that from the equation by letting my server go straight through to the internet, and it still did not work.

In reply to William Machin

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

by Jon Ware -

Add me to the list of people having this same issue. Any tips?

In reply to William Machin

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

by William Machin -

Is there anyone out there in Moodle land that can shed some light on this problem?

It really is holding us back with our development of Moodle, as without access to our Google Drive, we can't really proceed.

Cheers,
Will

In reply to William Machin

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

by Alan Bristow -

+1.

Total newb here. Only _just_ started researching the possibilities of using Moodle for all it's great at, but Google Drive or DropBox or YouTube for the heavy lifting of files (i.e. storage of 1.5GB videos etc rather than having a daft-large spec webserver and reinventing the wheel).

Off to go read up more on this and would be most interested to know if, for example, Google Drive can be a key part of our Moodle system.

Cheers!

In reply to Alan Bristow

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

by Shaun Daubney -

Hi guys, did this ever get sorted? I'm having the same problem. Doesn't seem to be the proxy as RSS and Drop Box both work.

Thanks

In reply to William Machin

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

by Nick Varney -

Same error here. We are sat behind a proxy but we are getting reliable data in for Google calendars for example.

Presumably the Google Drive repo is working for some folk? 

In reply to William Machin

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

by Matthew Becker -

Having the same issue. Has anyone had any luck?

In reply to Matthew Becker

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

by Jorge Merino -

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

In reply to William Machin

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

by Chris Annable -

Hi guys,

I had the same problem. I seem to have fixed it by replacing my 2.8 version of the file: /lib/google/curlio.php with the 2.9 version. At this stage I am unsure if this version will cause other issues with the newer version of this file on the older Moodle, but so far it seems promising.

Hope this helps!!

-Chris