请复制代码,切换到应用程序并将其粘贴到那里,但是在哪里?
Posted
技术标签:
【中文标题】请复制代码,切换到应用程序并将其粘贴到那里,但是在哪里?【英文标题】:Please copy code, switch to application and paste it there but where? 【发布时间】:2018-05-16 06:57:56 【问题描述】:我正在使用 oAuth2.0 从我的 Gmail 帐户中读取电子邮件。这是我的代码
oauthswift = OAuth2Swift(
consumerKey: "242468529977-xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
consumerSecret: "GfGVl_xxxxxxxxxxmjnAX",
authorizeUrl: "https://accounts.google.com/o/oauth2/auth",
accessTokenUrl: "https://accounts.google.com/o/oauth2/token",
responseType: "code"
)
oauthswift?.allowMissingStateCheck = true
let _ = oauthswift?.authorize(
withCallbackURL: URL(string: "urn:ietf:wg:oauth:2.0:oob")!, scope: "https://www.googleapis.com/auth/gmail.metadata", state: "",
success: credential, response, parameters in
let parameters = Dictionary<String, AnyObject>()
// Multi-part upload
print(credential)
print(response)
,
failure: error in
print("ERROR: \(error.localizedDescription)")
)
但在我允许后,它会显示这个屏幕
并说'请复制此代码并切换到应用程序并将其粘贴到那里。'但我不知道在哪里粘贴代码。
【问题讨论】:
Jecky consumerSecret: "",请删除您的键值并尝试它对我的工作 【参考方案1】:请用我的代码更新您的代码并用您的密钥替换秘密。我在我的项目中使用 OAuthSwift 库。 别忘了在你的项目中添加 URL 类型方案。 和 Make Role as Editor
let kClientID = ""
func doOAuthGoogle()
let oauthswift = OAuth2Swift(
consumerKey: kClientID,
consumerSecret: "",
authorizeUrl: "https://accounts.google.com/o/oauth2/auth",
accessTokenUrl: "https://accounts.google.com/o/oauth2/token",
responseType: "code"
)
// For googgle the redirect_uri should match your this syntax: your.bundle.id:/oauth2Callback
self.oauthswift = oauthswift
oauthswift.authorizeURLHandler = getURLHandler()
// in plist define a url schem with: your.bundle.id:
let _ = oauthswift.authorize(
withCallbackURL: URL(string: "com.cearsinfotech.GmailAttachements:/oauth2Callback")!, scope: "https://www.googleapis.com/auth/gmail", state: "GMAIL",
success: credential, response, parameters in
// self.showTokenAlert(name: "Gmail", credential: credential)
print(credential.oauthToken)
let jsonDict = try? response?.jsonObject()
print("SUCCESS: \(jsonDict)")
print(parameters)
let _ = oauthswift.client.get("https://www.googleapis.com/gmail/v3/about", success: response in
let jsonDict:NSDictionary = try! response.jsonObject() as! NSDictionary
print("SUCCESS: \(jsonDict)")
if let arrayMessages = jsonDict.value(forKey:"messages") as? NSArray
let dict = arrayMessages[2] as! NSDictionary
let id = dict.value(forKey: "id") as! String
let _ = oauthswift.client.get("https://www.googleapis.com/gmail/v1/users/me/messages/\(id)", success: response in
let jsonDict:NSDictionary = try! response.jsonObject() as! NSDictionary
print("SUCCESS: \(jsonDict)")
if let payload = jsonDict.value(forKey: "payload") as? NSDictionary
print(payload)
if let parts = payload.value(forKey: "parts") as? NSArray
print(parts)
let partDict = parts[0] as! NSDictionary
if let body = partDict.value(forKey: "body") as? NSDictionary
print(body)
, failure: error in
print(error)
)
, failure: error in
print(error)
)
,
failure: error in
print("ERROR: \(error.localizedDescription)")
//code=4/pYAZQTq2Y5nz0g0hZSAC4wC3AuQLzdJlW6pVjjXDFHM#
)
你必须要使用处理方法
//MARK:- Get URL -
func getURLHandler() -> OAuthSwiftURLHandlerType
guard let type = self.formData.data?.handlerType else
return OAuthSwiftOpenURLExternally.sharedInstance
switch type
case .external :
return OAuthSwiftOpenURLExternally.sharedInstance
case .`internal`:
if internalWebViewController.parent == nil
self.addChildViewController(internalWebViewController)
return internalWebViewController
case .safari:
#if os(ios)
if #available(iOS 9.0, *)
let handler = SafariURLHandler(viewController: self, oauthSwift: self.oauthswift!)
handler.presentCompletion =
print("Safari presented")
handler.dismissCompletion =
print("Safari dismissed")
return handler
#endif
return OAuthSwiftOpenURLExternally.sharedInstance
【讨论】:
以上是关于请复制代码,切换到应用程序并将其粘贴到那里,但是在哪里?的主要内容,如果未能解决你的问题,请参考以下文章