2017-01-13 25 views
2

현재 사용자를 OAuth2.0으로 인증하려고합니다. 현재 다음 라이브러리를 사용 중입니다 : https://github.com/p2/OAuth2. 여기 내 코드입니다 :Fitbit OAuth2.0 Token-request (Swift 3)

let oauth2 = OAuth2CodeGrant(settings: [ 
    "client_id": "******", //ID from Fitbit 
    "client_secret": "***************************", //Secret from Fitbit 
    "authorize_uri": "https://www.fitbit.com/oauth2/authorize", 
    "token_uri": "https://api.fitbit.com/oauth2/token", 
    "redirect_uris": ["http://google.com/"], 
    "scope": "profile settings activity nutrition heartrate location nutrition profile settings sleep social weight", 
    "secret_in_body": true, 
    "keychain": false,   
    ] as OAuth2JSON) 

그리고 여기 제 기능을 부여하는 것입니다 : 여기

func startAuthorization() { 
    oauth2.logger = OAuth2DebugLogger(.trace) 

    oauth2.authorize() { authParameters, error in 
     if let params = authParameters { 
      print("Authorized! Access token is in `oauth2.accessToken`") 
      print("Authorized! Additional parameters: \(params)") 
     } 
     else { 
      print("Authorization was cancelled or went wrong: \(error)") // error will not be nil 
     } 
    } 
} 

내 로그 추적의 :

[Debug] OAuth2: Starting authorization 
[Debug] OAuth2: No access token, checking if a refresh token is available 
[Debug] OAuth2: I don't have a refresh token, not trying to refresh 
[Debug] OAuth2: Opening authorize URL in system browser: https://www.fitbit.com/oauth2/authorize?state=35701F98&response_type=code&scope=profile+settings+activity+nutrition+heartrate+location+nutrition+profile+settings+sleep+social+weight&redirect_uri=http%3A%2F%2Fgoogle.com%2F&client_id=****** 

그것은 오류없이 완벽하게 작동하지만 액세스 토큰을 얻지 마십시오. 내가 찾은 모든 자습서는 Swift 2에 있습니다. 당신이 나를 도울 수 있기를 바랍니다!

답변

0

데모 응용 프로그램에 Fitbit에 대한 샘플이 있지만 도움이되는지 확신 할 수 없습니다. 약 2 개월 전에 마지막으로 업데이트되었으므로 Swift 3에 있어야합니다.하지만 그렇지 않은 경우 사전에 사과드립니다.

현재 위치 : 핏 비트

편집 : 나의 나쁜, 잘못된 라이브러리 ...

https://github.com/OAuthSwift/OAuthSwift/blob/master/Demo/Common/ViewController.swift

그들은 모두 핏 비트에 대한 코드와 핏 비트 2.

// 마크 검색을

https://github.com/OAuthSwift/OAuthSwift

:

내 대답은이 라이브러리했다3210

+0

Fitbit Oauth2 페이지에서 알 수 있습니다. 기본 응용 프로그램의 경우 인증 페이지가 기본 브라우저에서 열려야 함을 의미합니다. 네이티브 응용 프로그램은 사용자 지정 URL 스킴을 리디렉션 URI로 사용하여 사용자를 브라우저에서 권한을 요청하는 응용 프로그램으로 다시 리디렉션 할 수 있습니다. iOS 응용 프로그램이 Safari로 전환하는 대신 SFSafariViewController 클래스를 사용할 수 있습니다. WKWebView 또는 UIWebView 클래스의 사용은 금지됩니다. – fpeng

+1

인증 코드 부여 흐름은 다음 단계로 구성됩니다. 응용 프로그램이 사용자를 Fitbit의 인증 페이지로 리디렉션합니다. 사용자 동의에 따라 Fitbit은 사용자를 인증 코드를 URL 매개 변수로 사용하여 응용 프로그램의 리디렉션 URL로 리디렉션합니다. 응용 프로그램이 액세스 토큰 및 새로 고침 토큰에 대한 인증 코드를 교환합니다. 응용 프로그램이 액세스 토큰과 새로 고침 토큰을 저장합니다. 액세스 토큰을 사용하여 Fitbit API에 요청합니다. 사용자에게 다시 묻지 않고 액세스 토큰이 만료되면 새로 고침 토큰을 사용하여 새 액세스 토큰을 얻습니다. – fpeng

+0

이미이 라이브러리를 사용해 보았습니다. 그러나 나를 위해 일하지 않았다. :( – Moo123