【问题标题】:403 Error: disallowed_useragent403 错误:disallowed_useragent
【发布时间】:2017-03-09 14:54:40
【问题描述】:

我是移动应用程序开发的新手,我正在开发一个 ios 应用程序。我正在使用谷歌驱动器将文档文件从谷歌帐户获取到我的应用程序中。我完成了该任务并且工作正常。但是现在当我尝试对其进行身份验证时它无法正常工作它显示 403 错误:disallowed_useragent 在我的应用程序中。我对此进行了谷歌搜索,但有些令人困惑,我从中读到了stack overflow question,发现谷歌驱动器已更新,现在我不知道我是要重做任务还是必须更新任务才能登录才能完成它,请任何人建议我关于那个

提前感谢

【问题讨论】:

标签: ios google-drive-api


【解决方案1】:

按照谷歌驱动API快速入门后,在AppDelegate.m中添加这3行代码如下图所示..然后就可以正常工作了...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

    return YES;
}

【讨论】:

  • 谢谢。它完美而简单的解决方案。
  • 美观简洁
【解决方案2】:

在最近更改 Google OAuth 政策后,有一个解决此问题的方法。

在集成 Google Sign 并启用 Google Drive API 后,我能够使用 Google Drive API 来获取所有驱动器数据。我们只需要设置 GTLServiceDrive 的授权者,它是谷歌登录后获得的。

service.authorizer = user.authentication.fetcherAuthorizer()

这里是 Google GIDSignIn 的代码 sn-ps,然后是获取日历事件。

  import GoogleAPIClient
    import GTMOAuth2
    import UIKit
    import GoogleSignIn

    class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {

      private let kApiKey = "AIzaXXXXXXXXXXXXXXXXXXXXXXX"

      // If modifying these scopes, delete your previously saved credentials by
      // resetting the iOS simulator or uninstall the app.
      private let scopes = [kGTLAuthScopeDriveMetadataReadonly]
      private let service = GTLServiceDrive()

      override func viewDidLoad() {
          super.viewDidLoad()

          service.apiKey = kApiKey

          GIDSignIn.sharedInstance().uiDelegate = self
          GIDSignIn.sharedInstance().scopes = scopes
          GIDSignIn.sharedInstance().signIn()
          GIDSignIn.sharedInstance().delegate = self
      }


      func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

          if user != nil {
               print("\(user)")
               service.authorizer = user.authentication.fetcherAuthorizer()
               loadDriveFiles()
          }
      }

     // Construct a query and get a list of upcoming events from the user calendar
       func loadDriveFiles() {
            //Googly Drive fetch Query
       }

   }

【讨论】:

    【解决方案3】:

    Google 正在更改其 Oauth 政策,这样做的目的是没有本地 Web 视图启动 Oauth 流程,我也在使用 iOS 应用程序并且遇到同样的问题,您可能应该等待他们更新 SDK 和文档或找到另一种身份验证方式以使用用户信息。

    【讨论】:

      【解决方案4】:

      另一种方法是使用第三方 CloudRail SDK,它可以使用外部浏览器进行身份验证。这个blog post 正好涵盖了你提到的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        • 2018-04-24
        • 2022-10-30
        • 2017-08-10
        • 2023-03-12
        • 1970-01-01
        • 2023-03-07
        相关资源
        最近更新 更多