Swift3 Twitter友谊/创建api要求在swift中进行身份验证
我正在尝试发布一个跟踪用户的帖子,但仍然无法点击post api post 它显示: 错误:可选(错误域=TwitterApierDomain代码=220“请求 失败:禁止(403)“UserInfo={NSLocalizedFailureReason=Twitter API错误:您的凭据不允许访问此资源。 (代码220),TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=, NSLocalizedDescription=请求失败:禁止(403)}) 这是我的代码:::Swift3 Twitter友谊/创建api要求在swift中进行身份验证,swift3,Swift3,我正在尝试发布一个跟踪用户的帖子,但仍然无法点击post api post 它显示: 错误:可选(错误域=TwitterApierDomain代码=220“请求 失败:禁止(403)“UserInfo={NSLocalizedFailureReason=Twitter API错误:您的凭据不允许访问此资源。 (代码220),TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=, NSLocalizedDescription=请求失败:禁止(403
让twitterClient=TWTRAPIClient()
有人能帮我吗。提前感谢这是使用用户的twitter id或屏幕名称跟踪用户的简单方法: func使用SLComposer Twitter()获取以下信息{
错误很明显,它表示您没有凭据,请显示您的代码。let twitterClient=TWTRAPIClient()let StatuseShowEndpoint=“”var clientError:NSError?var request=twitterClient.urlRequest(withMethod:“POST”,url:StatuseShowEndpoint,参数:nil,错误:&clientError)request.addValue(“承载(twitterAccess)”,forHTTPHeaderField:“授权”)request.addValue(“gzip”,forHTTPHeaderField:“接受编码”)twitterClient.sendTwitterRequest(请求){(响应,数据,connectionError)in if connectionError!=nil{print(“错误:(connectionError)”)}else{do{print(“response::(response)”)在帖子上添加代码
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)")
}
}
}
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)
})
})
}