为什么我会从Google Drive Rest API禁止服务器回复? [英] Why am I getting server replied forbidden from google drive rest api?

查看:82
本文介绍了为什么我会从Google Drive Rest API禁止服务器回复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:

我正在尝试将我的Qt桌面应用程序与Google驱动器连接.现在,我的目标很简单,即从驱动器中获取所有文件的列表(以JSON形式)

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)

我到目前为止所做的事情.

我已按照本教程使用OAuth 2.0将您的Qt应用程序与Google Services连接,并为此创建了自己的包装器类.我遵循了所有步骤,直到调用了grant()函数.范围是 https://www.googleapis.com/auth/drive .到现在为止一切正常.

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.

我使用了从 QOAuthHttpServerReplyHandler 发出的 tokensReceived 信号,并存储了access_token.简而言之,我能够验证我的应用程序并获取访问令牌.现在对我来说,下一步是执行一个简单的get请求.这就是我所做的

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);
}

我遇到的错误:

回复我得到的是

传输 https://www.googleapis.com/drive/v3/files -服务器回复:禁止"

"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!


这次,作为回复接收方法,我 readAll(),而不是仅仅阅读 errorString(),我发现该api没有打开.因此,我从开发人员控制台打开了api.现在,在设置完之后,我现在将 errorString()设置为

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

传输 https://www.googleapis.com/drive/v3/about -服务器回复:错误的请求"

"Error transferring https://www.googleapis.com/drive/v3/about - server replied: Bad Request"

并从 reply-> readAll()返回

"{\ n \"错误\:{\ n \"错误\:[\ n {\ n \"域\:\"global \",\ n \"reason \":\必填",\ n \"message \":\此方法需要'fields'参数.\,\ n
\"locationType \":\参数\",\ n \"location \":\字段\" \ n} \ n],\ n \"code \":400,\ n \"message \":\字段"参数为此方法必需.\"\ n} \ n} \ n"

"{\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"


抱歉,为了测试响应,我使用的是" https://www.googleapis.com/drive/v3/about ",而不是" https://www.googleapis.com/驱动器/v3/文件".现在纠正它可以正常工作.

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.

推荐答案

禁止"最有可能意味着他们的访问令牌或应用令牌的方式有问题.

Forbidden most likely means theirs something wrong with the access token or the way your applying it.

最简单的测试方法是

https://www.googleapis.com/drive/v3/files?access_token=Yourtoken

您可以将其添加到您的代码中,也可以将其放置在Web浏览器中,因为它具有http get调用.

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());

此方法要求使用字段"参数.

这是Google Drive API中未记录的必填字段.您必须随所有请求一起发送fields = *.

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.

尝试并添加此

QNetworkRequest request(QUrl("https://www.googleapis.com/drive/v3/files?fields=*"));

这篇关于为什么我会从Google Drive Rest API禁止服务器回复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆