问题
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.
it is showing :
Error: Optional(Error Domain=TwitterAPIErrorDomain Code=220 "Request failed: forbidden (403)" UserInfo={NSLocalizedFailureReason=Twitter API error : Your credentials do not allow access to this resource. (code 220), TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=https://api.twitter.com/1.1/friendships/create.json?user_id=12345&follow=true, NSLocalizedDescription=Request failed: forbidden (403)})
this is my code :::
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
回答1:
this is the simple way to follow user with his/her twitter id or screen_name:
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)
})
})
}
来源:https://stackoverflow.com/questions/43580804/twitter-friendship-create-api-asking-for-authentication-in-swift