gpt4 book ai didi

ios - 无法读取数据,因为获取 Google 联系人时返回的格式不正确

转载 作者:行者123 更新时间:2023-11-30 12:39:26 24 4
gpt4 key购买 nike

我正在尝试在我的 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/emailhere@gmail.com/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>

最佳答案

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

关于ios - 无法读取数据,因为获取 Google 联系人时返回的格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42418471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com