如何使用本机 Google Drive iOS SDK 修复“403 - disallowed_useragent”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40601168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:47:46  来源:igfitidea点击:

How to fix a "403 - disallowed_useragent" with the native Google Drive iOS SDK?

iosgoogle-drive-api

提问by eipipuz

I'm following the instructions given at: https://developers.google.com/drive/ios/quickstart?ver=swift.

我正在按照以下说明操作:https://developers.google.com/drive/ios/quickstart?ver=swift

Yesterday it was working but this morning it seems to always fail. The weird part is that I'm using their native SDK. I don't know why does it believe I'm using a web-view. The exact error is:

昨天它正在工作,但今天早上它似乎总是失败。奇怪的是我使用的是他们的原生 SDK。我不知道为什么它相信我在使用网络视图。确切的错误是:

This user-agent is not permitted to make an OAuth authorization request to Google as it is classified as an embedded user-agent (also known as a web-view). Per our policy, only browser are permitted to make authorization requests to Google. We offer several libraries and samples for native apps to perform authorization request in the browser.

此用户代理不允许向 Google 发出 OAuth 授权请求,因为它被归类为嵌入式用户代理(也称为网络视图)。根据我们的政策,只有浏览器可以向 Google 提出授权请求。我们为原生应用程序提供了多个库和示例,以在浏览器中执行授权请求。

I cloned: https://github.com/googledrive/ios-quickeditorand got the same error.

我克隆了:https: //github.com/googledrive/ios-quickeditor并得到了同样的错误。

回答by marco

Here is the unfortunate answer: Yes, you are using the example from Google's iOS Quickstart, the "native SDK" as you call it. That's outdated, however, as it uses exactly the embedded web view that Google doesn't want you to use anymore.

这是不幸的答案:是的,您正在使用 Google 的iOS 快速入门中的示例,即您所说的“本机 SDK”。但是,这已经过时了,因为它完全使用了 Google 不想让您再使用的嵌入式 Web 视图。

If it did work before, but has stopped doing so all of a sudden (like I experienced myself), it may have to do with you changing something related to the client ID. For client IDs that exist for some time, there seems to be a longer grace period until Google hard-blocks the web view use (there should still be just some warning for now, I understand). A new client ID I created recently for unrelated reasons, seems to have caused just that, getting me stuck in having to replace Google Drive authentication code for a new release at short notice.

如果它以前确实有效,但突然停止了(就像我自己经历的那样),则可能与您更改了与客户端 ID 相关的内容有关。对于存在一段时间的客户端 ID,似乎有更长的宽限期,直到 Google 硬阻止 Web 视图的使用(我理解,目前应该仍然有一些警告)。我最近出于不相关的原因创建了一个新的客户端 ID,这似乎就是造成这种情况的原因,让我不得不在短时间内为新版本替换 Google Drive 身份验证代码。

As to the options (that the so far only other respondent to your question lists from the Google blog post), after a bit of research GTMAppAuth seems to be the most viable option to me, as I am not interested in complete user sign-in and -out. But I may be wrong there.

至于选项(到目前为止,只有 Google 博客文章中您的问题列表的其他答复者),经过一些研究 GTMAppAuth 对我来说似乎是最可行的选择,因为我对完整的用户登录不感兴趣和-出。但我可能错了。

GTMAppAuthhas sample code for iOS, yet just in Objective-C. I have yet to make it through that. In general, your code logic will change like so: Your current solution does authentication purely within your app. It's asynchronous, but doesn't leave your app. The new solution requires you to initiate authentication within your app while preparing and remembering your callback functionality. The authentication initiation then opens a browser outside, which, once done causes a URL callback in your app delegate. In your app delegate, you then check for your prepared callback and, depending on the result status of the outside call, notify the user about success or failure of the outside authentication.

GTMAppAuth有适用于 iOS 的示例代码,但只是在 Objective-C 中。我还没有做到这一点。通常,您的代码逻辑会发生如下变化:您当前的解决方案仅在您的应用程序中进行身份验证。它是异步的,但不会离开您的应用程序。新的解决方案要求您在准备和记住您的回调功能的同时在您的应用程序中启动身份验证。身份验证启动然后在外部打开一个浏览器,一旦完成,将在您的应用程序委托中引发 URL 回调。在您的应用委托中,您检查准备好的回调,并根据外部调用的结果状态通知用户外部身份验证成功或失败。

回答by Shiva Krishna Kongara

try this, it will works for "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

试试这个,它适用于“Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36”

Google restrict the authentication from webviews. it's accepting safari webkit useragent .override useragent with above useragent string it will works.

Google 限制来自 webview 的身份验证。它接受 safari webkit useragent .override useragent with above useragent string 它会起作用。

回答by Vladyslav Zavalykhatko

Add this code to your AppDelegate.m(assuming it is in ObjC):

将此代码添加到您的AppDelegate.m(假设它在 ObjC 中):

NSString *userAgent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/603.1.23 (KHTML, like Gecko) Version/10.0 Mobile/14E5239e Safari/602";

// set default user agent
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:userAgent,@"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:(dictionary)];

回答by Tmm

You can solve this issue by using the 3rd party SDK by CloudRail which works fine for Google Drive and the new authentication restrictions. This tutorialexactly shows you how to use an external browser rather than a web -view.

您可以通过使用 CloudRail 的 3rd 方 SDK 来解决此问题,该 SDK 适用于 Google Drive 和新的身份验证限制。本教程准确地向您展示了如何使用外部浏览器而不是 Web 视图。