Reputation: 41
i build project swift with OAuth and i already got Acces Token from my API Login and then i want get data from API JSon with my acces token. so, how to use OAuth get request http method with OAuth and my acces token
i have json parse standar without OAuth
let url = NSURL(string: "https://conversation.8villages.com/1.0/contents/articles?state=published")
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in
guard error == nil && data != nil else {
print("error", error!)
return
}
let httpStatus = response as? HTTPURLResponse
if httpStatus!.statusCode == 200
{
if data?.count != 0
{
let responString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(responString)
}
else{
print("No got data from URL")
}
}
else
{
print("error httpstatus code is ", httpStatus!.statusCode)
}
}
task.resume()
i'm used Librabry https://github.com/OAuthSwift/OAuthSwift
in there i got Signed Request (Readme.md) like this
oauthswift.client.get("https://api.linkedin.com/v1/people/~",
success: { response in
let dataString = response.string
print(dataString)
},
failure: { error in
print(error)
}
)
but im confused how to add my Acces Token, Consumer Key and My Consumer Secret to acces my API JSON
Upvotes: 1
Views: 1204
Reputation: 5
I'm not familiar with OAuthSwift framework but I guess the error is in the following row:
request.addValue("application/json", forHTTPHeaderField: "Authorization")
Maybe it should be:
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(tokenType + " " + accessToken, forHTTPHeaderField: "Authorization")
Where tokenType
for a OAuth Authorization Code grant type always is the string Bearer
and accessToken
is of course the access token you have got from earlier.
EDIT: Compare with the Microsoft Azure AD:
GET /v1.0/me/messages
Host: https://graph.microsoft.com
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUz...
Upvotes: 0
Reputation: 94
If you have got the access token, it seems you have already used your consumer key and consumer secret and you are authenticated. You need to pass the bearer access token in the authorization header of API get call.
Upvotes: 0