TMC is an independent, primarily volunteer organization that relies on ad revenue to cover its operating costs. Please consider whitelisting TMC on your ad blocker and becoming a Supporting Member. For more info: Support TMC

REST API SWIFT

Discussion in 'Model S: User Interface' started by NicB72, Jul 15, 2016.

  1. NicB72

    NicB72 Supporting Member

    Joined:
    Jun 25, 2016
    Messages:
    98
    Location:
    Concord, MA
    So, I have read through all 81 pages of the REST API thread and not found an answer.

    I am new to SWIFT and trying to use the REST API. Getting stuck at the auth component.
    I have read Tesla Model S JSON API · Apiary

    and I know its....

    POST https://owner-api.teslamotors.com/oauth/token
    Attributes
    • grant_type
    • client_id
    • client_secret
    • email
    • password

    So I am doing the usual

    let url = NSURL(string: "https://owner-api.teslamotors.com/oauth/token")!
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.setValue("password", forHTTPHeaderField: "grant_type")

    and setting the rest of the header values
    But I get back...

    "Www-Authenticate" = "Bearer realm=\"Doorkeeper\", error=\"invalid_request\", error_description=\"The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.\"";



    Optional("{\"error\":\"invalid_request\",\"error_description\":\"The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.\"}")
     
  2. fact200

    fact200 Member

    Joined:
    Sep 25, 2014
    Messages:
    107
    Location:
    San Francisco
    Those parameters are passed in as normal form/url encoded parameters and not in the header...
     
  3. GaryREM

    GaryREM Member

    Joined:
    Jul 16, 2013
    Messages:
    565
    Location:
    Fairfax, VA
    // Using OS X Playground

    import XCPlayground
    import Cocoa
    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

    let url = NSURL(string: "https://owner-api.teslamotors.com/oauth/token")!
    let request = NSMutableURLRequest(URL: url)
    var stringPost = "grant_type=password&client_id=aaaaa&client_secret=bbbbbb&[email protected]&password=zzzzzz"
    request.HTTPMethod = "POST"
    let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
    request.HTTPBody = data
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) { data, response, error in
    if let response = response, data = data {
    print(response)
    print(String(data: data, encoding: NSUTF8StringEncoding))
    } else {
    print(error)
    }
    }

    task.resume()

    This seems to work with the right client_id, client_secret, email and password as @fact200 indicated.
     
  4. NicB72

    NicB72 Supporting Member

    Joined:
    Jun 25, 2016
    Messages:
    98
    Location:
    Concord, MA
    Thanks guys. Been writing too much code late at night and was stuck thinking header as thats what the other apps were doing.
     

Share This Page

  • About Us

    Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.
  • Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.


    SUPPORT TMC