【问题标题】:Set like with Instagram api [closed]使用 Instagram api 设置 [关闭]
【发布时间】:2017-07-06 00:23:08
【问题描述】:

我正在使用 Instagram 应用构建一个 ios 应用,我想在帖子上设置类似

curl -F 'access_token=ACCESS-TOKEN' \
https://api.instagram.com/v1/media/{media-id}/likes

如何在 swift 中使用 Alamofire 发出这个 curl 请求?

【问题讨论】:

    标签: ios swift api instagram alamofire


    【解决方案1】:

    来自Alamofire tutorial on Github:

    以下是使用 HTTP 标头创建 POST 请求的方法:

    let headers: HTTPHeaders = [
        "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
        "Accept": "application/json"
    ]
    
    Alamofire.request("https://httpbin.org/headers", headers: headers).responseJSON { response in
        debugPrint(response)
    }
    

    现在,为 Instagram API 创建一个 POST:

    let headers: HTTPHeaders = [
        "access_token": \(YOUR_ACCESS_TOKEN)
    ]
    
    Alamofire.request("https://api.instagram.com/v1/media/\(media-id)/likes", headers: headers).responseJSON { response in
        print(response)
    }
    
    // you could also explicitly define the request as a POST
    Alamofire.request("https://api.instagram.com/v1/media/\(media-id)/likes", method: .post, headers: headers)
    

    编辑#1:

    略微更改代码以反映 OP 的工作解决方案。

    let headers: HTTPHeaders = [
        "access_token": \(YOUR_ACCESS_TOKEN)
    ]
    
    Alamofire.request("https://api.instagram.com/v1/media/\(media-id)/likes", method: .post, parameters: header).responseJSON { response in
        print(response)
    }
    

    【讨论】:

    • 我发出请求,但它说它缺少访问令牌,我正在使用带有响应 json 的 alamofire
    • 没关系我自己做的。我已将 headers 更改为 parameters 并添加了 method: .post 并且它开始工作了!
    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多