问题
My Goal:
I am trying to connect my Qt desktop app with google drive. Right now my goal is simple i.e get list of all files from my drive (In JSON form)
What I have done so far.
I have followed this tutorial Connecting your Qt application with Google Services using OAuth 2.0 and created my own wrapper class for it. I have followed all the steps till grant() function is called. The scope is https://www.googleapis.com/auth/drive
. Everything is working fine till now.
I used the signal tokensReceived
emitted from QOAuthHttpServerReplyHandler
and stored the access_token. So in short I am able to authenticate my application and get the access token. Now the next step for me is to perform a simple get request. This is what I have done
void Google_Drive::Send_Request()
{
QNetworkRequest request(QUrl("https://www.googleapis.com/drive/v3/files"));
request.setRawHeader("Authorization", QByteArray("Bearer ")+mAccessToken);
mManager.get(request);
}
Error I am getting:
Reply I am getting is
"Error transferring https://www.googleapis.com/drive/v3/files - server replied: Forbidden"
Please let me know what I am missing or am I doing something wrong. Also if any additional detail is required let me know. Thank you!
Edit 1:
In reply recieving method this time I readAll()
instead of just reading errorString()
and I found that the api was not turned on. So I turned On the api from developer console. Now I after setting that Now I am getting the errorString()
as
"Error transferring https://www.googleapis.com/drive/v3/about - server replied: Bad Request"
and from reply->readAll()
returns
"{\n \"error\": {\n \"errors\": [\n {\n \"domain\": \"global\",\n \"reason\": \"required\",\n \"message\": \"The 'fields' parameter is required for this method.\",\n
\"locationType\": \"parameter\",\n \"location\": \"fields\"\n }\n ],\n \"code\": 400,\n \"message\": \"The 'fields' parameter is required for this method.\"\n }\n}\n"
Edit 2:
My apologies, to test the response I was using "https://www.googleapis.com/drive/v3/about" instead of "https://www.googleapis.com/drive/v3/files". Now correcting it works fine.
回答1:
Forbidden most likely means theirs something wrong with the access token or the way your applying it.
Easiest way to test it is to do this
https://www.googleapis.com/drive/v3/files?access_token=Yourtoken
You can either add it to your code or just put it in a web browser since its a http get call.
If the access token works then you know there is something wrong with the way you are applying the authorization header.
QString headerData = "Bearer " + mAccessToken;
request.setRawHeader("Authorization", headerData.toLocal8Bit());
The 'fields' parameter is required for this method.
This is an undocumented required field in the google drive api. You must send fields=* with all your requests.
fields Selector specifying a subset of fields to include in the response.
For more information, see the partial response section in the Performance Tips document. Use for better performance.
try and add this
QNetworkRequest request(QUrl("https://www.googleapis.com/drive/v3/files?fields=*"));
来源:https://stackoverflow.com/questions/52181208/why-am-i-getting-server-replied-forbidden-from-google-drive-rest-api