【问题标题】:ios Instagram api POST Relationship Endpoints using Alamofire - Errorios Instagram api POST 关系端点使用 Alamofire - 错误
【发布时间】:2018-05-18 11:50:54
【问题描述】:

我在我的应用中使用 swift

我正在尝试使用 Alamofire 发布具有范围(“follower_list+relationships”)的关系(关注)

注意:我使用沙盒用户进行测试

使用此代码

let userid = "testAccount"
    let urlString = "https://api.instagram.com/v1/users/\(userid)/relationship?access_token=\(INSTAGRAM_IDS.INSTAGRAM_Token)&action=follow"

    Alamofire.request(urlString).responseJSON { response in switch response.result {

        case .success(let JSON):

        let response = JSON as! NSDictionary
        let userModel = response

            print("SUCCESS")
            print(userModel)

        case .failure(let error):

            print("ERROR")
            print(error)
        }
    }

也试过这个

let userid = "testAccount"
    let urlString = "https://api.instagram.com/v1/users/\(userid)/relationship?access_token=\(INSTAGRAM_IDS.INSTAGRAM_Token)"
    let parameters: Parameters = ["action": "follow"]

    Alamofire.request(urlString, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { response in switch response.result {

        case .success(let JSON):

        let response = JSON as! NSDictionary
        let userModel = response

            print("SUCCESS")
            print(userModel)

        case .failure(let error):

            print("ERROR")
            print(error)
        }
    }

但仍然收到此消息??

错误 responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "字符 0 周围的值无效。" UserInfo={NSDebugDescription=字符 0 周围的值无效。}))

有什么想法吗??

【问题讨论】:

    标签: ios swift api instagram alamofire


    【解决方案1】:

    我发现了问题......我一直在使用帐户用户名而不是用户 ID 有区别……我的错

    您可以从(搜索)Endpoint api 或使用类似的网站获取用户 ID https://smashballoon.com/instagram-feed/find-instagram-user-id/

    您输入用户名,您将获得 ID

    【讨论】:

      【解决方案2】:

      如果您尝试发布 json,我怀疑您需要使用 encoding: JSONEncoding.default 更改编码

      JSONEncoding 类型创建参数对象的 JSON 表示,它被设置为请求的 HTTP 正文。编码请求的 Content-Type HTTP 标头字段设置为 application/json。

      github上使用

      【讨论】:

      • 嗨布赖恩.....感谢您的回复.....我已将其更改为编码:JSONEncoding.default 但仍然收到相同的消息!
      • @Nasser 老实说,我以前没有使用过这个 api,但我刚刚用模拟的 api 和 JSON 编码测试了你的代码。它成功返回,所以你的语法没问题。不过,您似乎没有得到有效的 JSON。我还检查了 api 文档,您的请求似乎格式正确。您的访问令牌和用户 ID 是否绝对正确?
      • 是的,我传递的访问令牌和用户 ID 是正确的
      猜你喜欢
      • 2015-08-06
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多