2017-12-02 22 views
6

Gmail 계정에서 이메일을 읽으려면 oAuth2.0을 사용하고 있습니다. 그리고 여기 내 코드코드를 복사하고 애플리케이션으로 전환하여 붙여 넣으십시오.

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

하지만 난 권한을 허용 후 나에게이 화면

enter image description here

을 보여주고 을 말한다되어 '이 코드를 복사하여 애플리케이션으로 전환하여 붙여 넣으세요.'하지만 코드를 붙여 넣을 위치를 모르겠습니다.

+0

Jecky consumerSecret 처리기 방법을 사용하려면 있어야합니다 ""당신의 키 값을 제거하고 그래서 나를 위해 그것을 작업을 시도하십시오 –

답변

3

내 코드로 코드를 업데이트하고 비밀 키로 대체하십시오. 내 프로젝트에서 OAuthSwift 라이브러리를 사용하고 있습니다. 또한 프로젝트에 URL 유형 구성표를 추가하는 것을 잊지 마십시오. 및 편집기 등 만들기 역할

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 
     } 


    }