ios Instagram api POST 使用 Alamofire 的关系端点 - 错误

ios Instagram api POST Relationship Endpoints using Alamofire - Error

我在我的应用程序中使用 swift

我正在尝试使用 Alamofire post 与范围 ("follower_list+relationships") 建立关系 (Follow)

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

使用此代码

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

但仍然收到这条消息 ??

ERROR responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))

有什么想法吗??

如果您尝试 post json 我怀疑您需要使用 encoding: JSONEncoding.default

更改编码

The JSONEncoding type creates a JSON representation of the parameters object, which is set as the HTTP body of the request. The Content-Type HTTP header field of an encoded request is set to application/json.

github

上的使用

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

您可以从(搜索)端点获取用户 ID api 或者使用像这样的网站 https://smashballoon.com/instagram-feed/find-instagram-user-id/

你输入用户名你会得到ID