How to Get HTTP request JSON

Discussion in 'iOS Programming' started by fermendkis, Apr 3, 2017.

Tags:
  1. fermendkis macrumors newbie

    fermendkis

    Joined:
    Apr 3, 2017
    Location:
    Indonesia
    #1
    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
     
  2. mds1256 macrumors regular

    Joined:
    Apr 9, 2011
    #2
    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.
     
  3. fermendkis thread starter macrumors newbie

    fermendkis

    Joined:
    Apr 3, 2017
    Location:
    Indonesia
    #3
    so, what must i do to add my value Token, Consumer Key, Consumer Secret ?
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    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.
     

Share This Page