使用 Google API 缩短 url,Swift 中的 AFNetworking
Get url shortened with Google API, AFNetworking in Swift
在Google文档(https://developers.google.com/url-shortener/v1/getting_started)中,要使用Google URL缩短器,我应该提出如下要求:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}
他们还说我必须进行身份验证:
"Every request your application sends to the Google URL Shortener API
needs to identify your application to Google. There are two ways to
identify your application: using an OAuth 2.0 token (which also
authorizes the request) and/or using the application's API key."
我选择 public API 密钥作为身份验证方法:我为我的 iOS 应用程序创建了一个 public 密钥。然后我使用下面的代码来POST(AFNetworking,使用Swift):
func getShortURL(longURL: String){
let manager = AFHTTPRequestOperationManager()
let params = [
"longUrl": longURL
]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key={my_key_inserted}", parameters: params, success: {
(operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error while requesting shortened: " + error.localizedDescription)
})
}
但是,我得到了日志:请求缩短时出错:请求失败:请求错误 (400)。
请告诉我如何解决它。
您缺少的是为此请求设置正确的 AFNetworking 序列化程序。
由于 Google 响应在 JSON 中,您应该使用 AFJSONRequestSerializer
。
像这样添加manager.requestSerializer = AFJSONRequestSerializer()
:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer()
let params = ["longUrl": "MYURL"]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=MYKEY", parameters: params, success: {(operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
}, failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error while requesting shortened: " + error.localizedDescription)
})
在Google文档(https://developers.google.com/url-shortener/v1/getting_started)中,要使用Google URL缩短器,我应该提出如下要求:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}
他们还说我必须进行身份验证:
"Every request your application sends to the Google URL Shortener API needs to identify your application to Google. There are two ways to identify your application: using an OAuth 2.0 token (which also authorizes the request) and/or using the application's API key."
我选择 public API 密钥作为身份验证方法:我为我的 iOS 应用程序创建了一个 public 密钥。然后我使用下面的代码来POST(AFNetworking,使用Swift):
func getShortURL(longURL: String){
let manager = AFHTTPRequestOperationManager()
let params = [
"longUrl": longURL
]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key={my_key_inserted}", parameters: params, success: {
(operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error while requesting shortened: " + error.localizedDescription)
})
}
但是,我得到了日志:请求缩短时出错:请求失败:请求错误 (400)。
请告诉我如何解决它。
您缺少的是为此请求设置正确的 AFNetworking 序列化程序。
由于 Google 响应在 JSON 中,您应该使用 AFJSONRequestSerializer
。
像这样添加manager.requestSerializer = AFJSONRequestSerializer()
:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer()
let params = ["longUrl": "MYURL"]
manager.POST("https://www.googleapis.com/urlshortener/v1/url?key=MYKEY", parameters: params, success: {(operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
}, failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error while requesting shortened: " + error.localizedDescription)
})