2017-02-23 79 views
-1

我想要檢索我的swift應用程序中的goole聯繫人,並收到下面的錯誤消息。用戶成功唱歌英寸數據無法被讀取,因爲它獲取谷歌聯繫人時返回的格式不正確

func getUserContacts(email : String){ 
     let urlString = "http://www.google.com/m8/feeds/contacts/\(email)/full" 
     Alamofire.request(urlString, method: .get) 
      .responseJSON { response in 

      switch response.result { 
      case .success(let JSON): 
       print(JSON as! NSDictionary) 
      case .failure(let error): 
       print(error.localizedDescription) 
      } 
    } 
} 

(LLDB)PO 「http://www.google.com/m8/feeds/contacts/(email)/full」 「http://www.google.com/m8/feeds/contacts/[email protected]/full

(lldb) po error.localizedDescription 
    "JSON could not be serialized because of error:\nThe data couldn’t be read because it isn’t in the correct format." 

(lldb) po response 
    ▿ [Request]: GET http://www.google.com/m8/feeds/contacts/emailhere.com/full 
    [Response]: <NSHTTPURLResponse: 0x170033da0> { URL: http://www.google.com/m8/feeds/contacts/emailhere.com/full } { status code: 401, headers { 
     "Cache-Control" = "private, max-age=0"; 
     "Content-Encoding" = gzip; 
     "Content-Length" = 8761; 
     "Content-Type" = "text/html; charset=utf-8"; 
     Date = "Thu, 23 Feb 2017 14:19:11 GMT"; 
     Expires = "Thu, 23 Feb 2017 14:19:11 GMT"; 
     Server = GSE; 
     "X-Content-Type-Options" = nosniff; 
     "X-Frame-Options" = SAMEORIGIN; 
     "X-XSS-Protection" = "1; mode=block"; 
    } } 
    [Data]: 11875 bytes 
    [Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) 
    [Timeline]: Timeline: { "Request Start Time": 509552351.480, "Initial Response Time": 509552351.663, "Request Completed Time": 509552351.665, "Serialization Completed Time": 509552351.667, "Latency": 0.183 secs, "Request Duration": 0.185 secs, "Serialization Duration": 0.002 secs, "Total Duration": 0.187 secs } 
     ▿ request : Optional<URLRequest> 
     ▿ some : http://www.google.com/m8/feeds/contacts/emailhere.com/full 
      ▿ url : Optional<URL> 
      ▿ some : http://www.google.com/m8/feeds/contacts/emailhere.com/full 
      - cachePolicy : 0 
      - timeoutInterval : 60.0 
      - mainDocumentURL : nil 
      - networkServiceType : __ObjC.NSURLRequest.NetworkServiceType 
      - allowsCellularAccess : true 
      ▿ httpMethod : Optional<String> 
      - some : "GET" 
      ▿ allHTTPHeaderFields : Optional<Dictionary<String, String>> 
      - some : 0 elements 
      - httpBody : nil 
      - httpBodyStream : nil 
      - httpShouldHandleCookies : true 
      - httpShouldUsePipelining : false 
     ▿ response : Optional<NSHTTPURLResponse> 
     ▿ data : Optional<Data> 
     ▿ some : 11875 bytes 
      - count : 11875 
      ▿ pointer : 0x0000000104e24000 
      - pointerValue : 4376903680 
     ▿ result : FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) 
     ▿ failure : AFError 
      ▿ responseSerializationFailed : ResponseSerializationFailureReason 
     ▿ timeline : Timeline: { "Request Start Time": 509552351.480, "Initial Response Time": 509552351.663, "Request Completed Time": 509552351.665, "Serialization Completed Time": 509552351.667, "Latency": 0.183 secs, "Request Duration": 0.185 secs, "Serialization Duration": 0.002 secs, "Total Duration": 0.187 secs } 
     - requestStartTime : 509552351.480039 
     - initialResponseTime : 509552351.66287899 
     - requestCompletedTime : 509552351.66534299 
     - serializationCompletedTime : 509552351.66746801 
     - latency : 0.18283998966217041 
     - requestDuration : 0.18530398607254028 
     - serializationDuration : 0.0021250247955322266 
     - totalDuration : 0.18742901086807251 
     ▿ _metrics : Optional<AnyObject> 
+0

可以格式化所有的問題? –

+0

狀態碼:401沒有被授權,所以也許有什麼失敗與您的身份驗證。你有回調建議你的認證是好的嗎? –

+0

我使用位於此處的Google API來獲取我的聯繫人https://developers.google.com/google-apps/contacts/v3/ – user1079052

回答

0
my problem was a permissions problem. I added GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/contacts.readonly") and gave my app access in https://myaccount.google.com/security 

then this worked: 
let urlString = "https://www.google.com/m8/feeds/contacts/default/full?access_token=\(GIDSignIn.sharedInstance().currentUser.authentication.accessToken!)&max-results=\(999)&alt=json&v=3.0" 


Alamofire.request(urlString, method: .get) 
     .validate(contentType: ["application/json"]) 
     .responseJSON { response in 

      switch response.result { 
      case .success(let JSON): 
      var json = JSON as! NSDictionary 
       print(JSON as! NSDictionary) 
      case .failure(let error): 
       print(error.localizedDescription) 
      } 
    } 
相關問題