Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fermendkis

macrumors newbie
Original poster
Apr 3, 2017
2
0
Indonesia
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

Code:
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

Code:
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
 

mds1256

macrumors regular
Apr 9, 2011
167
43
Usually they are added as either a field in the body or as a header field.

The line where you have:

request.addValue("application/json", forHTTPHeaderField: "Authorization")

That doesn't seem correct to me, the value for application/json would normally go into content-type header and not the authorization header. The authorization header is usually where your access token would go.
 

fermendkis

macrumors newbie
Original poster
Apr 3, 2017
2
0
Indonesia
Usually they are added as either a field in the body or as a header field.

The line where you have:

request.addValue("application/json", forHTTPHeaderField: "Authorization")

That doesn't seem correct to me, the value for application/json would normally go into content-type header and not the authorization header. The authorization header is usually where your access token would go.

so, what must i do to add my value Token, Consumer Key, Consumer Secret ?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
The answer to those questions is different for different servers. You need to look at the documentation for this server. Most likely appID and appSecret are sent when the user authenticates and the token is sent on all further requests. But there are different ways to do this so you need to find out from the server documentation.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.