本文介绍了Twitter Friendship/create api 要求快速进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布关注用户,但仍然无法点击发布 api POST https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true.

I am trying post a follow the user but am still not able to hit post api POST https://api.twitter.com/1.1/friendships/create.json?user_id=1401881&follow=true.

它显示:

错误:可选(错误域=TwitterAPIErrorDomain 代码=220请求失败:禁止(403)" UserInfo={NSLocalizedFailureReason=TwitterAPI 错误:您的凭据不允许访问此资源.(代码 220),TWTRNetworkingStatusCode=403,NSErrorFailingURLKey=https://api.twitter.com/1.1/friendships/create.json?user_id=12345&follow=true,NSLocalizedDescription=请求失败:禁止 (403)})

这是我的代码 :::

this is my code :::

让 twitterClient = TWTRAPIClient()

let twitterClient = TWTRAPIClient()

    let statusesShowEndpoint = "https://api.twitter.com/1.1/friendships/create.json?user_id=852067343241027587&follow=true"
    //let params = ["user_id":"\(userId)","follow":"true","screen_name": "Deploables1"]
    var clientError : NSError?

    var request = twitterClient.urlRequest(withMethod: "POST", url: statusesShowEndpoint, parameters: nil, error: &clientError)
    request.addValue("Bearer \(twitterAccess)", forHTTPHeaderField: "Authorization")
    request.addValue("gzip", forHTTPHeaderField: "Accept-Encoding")

    request.addValue("client_credentials", forHTTPHeaderField: "grant_type")
    request.addValue("application/x-www-form-urlencoded;charset=UTF-8", forHTTPHeaderField: "Content-Type")
    twitterClient.sendTwitterRequest(request) { (response, data, connectionError) in
        if connectionError != nil {
            print("Error: \(connectionError)")
        }else {

            do {
                print("response ::\(response)")



                let json:NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
                print("json: \(json)")



            } catch let jsonError as NSError {
                print("json error: \(jsonError.localizedDescription)")
            }





        }
    }

谁能帮我解决这个问题.提前致谢

can anyone help me out for this. Thanks in advance

推荐答案

这是使用他/她的 twitter id 或 screen_name 关注用户的简单方法:

this is the simple way to follow user with his/her twitter id or screen_name:

func gettingFollowUsingSLComposerTwitter() {

func gettingFollowUsingSLComposerTwitter() {

let accountStore = ACAccountStore()
let twitterType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)

accountStore.requestAccessToAccounts(with: twitterType, options: nil,
                                     completion: { (isGranted, error) in
    guard let userAccounts = accountStore.accounts(with: twitterType),
        userAccounts.count > 0 else { return }
    guard let firstActiveTwitterAccount = userAccounts[0] as? ACAccount else { return }

    // post params
    var params = [AnyHashable: Any]() //NSMutableDictionary()
        params["screen_name"] = "Deploables1"
        params["follow"] = "true"

    // post request
    guard let request = SLRequest(forServiceType: SLServiceTypeTwitter,requestMethod: SLRequestMethod.POST,
                                                                      url: URL(string: "https://api.twitter.com/1.1/friendships/create.json"),
                                                                      parameters: params) else { return }
    request.account = firstActiveTwitterAccount

    // execute request
      request.perform(handler: { (data, response, error) in
    print(response?.statusCode)
    print(error?.localizedDescription)

      })
})

}

这篇关于Twitter Friendship/create api 要求快速进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 06:52