使用 Google API 缩短 url,Swift 中的 AFNetworking [英] Get url shortened with Google API, AFNetworking in Swift

查看:31
本文介绍了使用 Google API 缩短 url,Swift 中的 AFNetworking的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Google 文档中(https://developers.google.com/url-shortener/v1/getting_started),要使用 Google URL 缩短器,我应该提出如下请求:

In Google documentation (https://developers.google.com/url-shortener/v1/getting_started), to use Google URL shortener, I should make a request as below:

POST https://www.googleapis.com/urlshortener/v1/url

内容类型:应用程序/json

Content-Type: application/json

{"longUrl": "http://www.google.com/"}

{"longUrl": "http://www.google.com/"}

他们还说我必须进行身份验证:

They also stated that I will have to authenticate:

您的应用程序发送到 Google URL Shortener API 的每个请求需要向 Google 识别您的应用程序.有两种方法可以识别您的应用程序:使用 OAuth 2.0 令牌(也授权请求)和/或使用应用程序的 API 密钥."

"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."

我选择公共 API 密钥作为验证方法:我为我的 iOS 应用创建了一个公共密钥.然后我使用以下代码进行 POST(AFNetworking,使用 Swift):

I chose public API key as a method to authenticate: I create a public key for my iOS app. Then I use the following code to POST (AFNetworking, using 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).

However, I got the log: Error while requesting shortened: Request failed: bad request (400).

请告诉我如何修复它.

推荐答案

您缺少的是为此请求设置正确的 AFNetworking 序列化程序.

What you are missing is setting the right AFNetworking serializer for this request.

由于 Google 响应采用 JSON,您应该使用 AFJSONRequestSerializer.

Since the Google response is in JSON, you should use 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 API 缩短 url,Swift 中的 AFNetworking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆