2017-12-02 18 views
6

मैं अपने जीमेल खाते से ईमेल पढ़ने के लिए 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 लाइब्रेरी का उपयोग कर रहा हूं। अपनी परियोजना में यूआरएल प्रकार योजनाएं भी जोड़ना न भूलें। और संपादक के रूप में मेक भूमिका

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 
     } 


    } 
संबंधित मुद्दे