2017-07-28 79 views
0

我試圖在iOS中實現AppAuth。基本實施已經完成。似乎一切工作正常。但我沒有按預期收到代幣。即時得到錯誤Error Domain=org.openid.appauth.general Code=-4OAuth錯誤一般代碼4 ​​Swift - iOS的AppAuth

let authorizationEndpoint : NSURL = NSURL(string: "https://accounts.google.com/o/oauth2/v2/auth")! 
    let tokenEndpoint : NSURL = NSURL(string: "https://www.googleapis.com/oauth2/v4/token")! 

    let configuration = OIDServiceConfiguration(authorizationEndpoint: authorizationEndpoint as URL, tokenEndpoint: tokenEndpoint as URL) 

    let request = OIDAuthorizationRequest.init(configuration: configuration, clientId: "<MyTOKEN>", scopes: [OIDScopeOpenID], redirectURL: URL(string: "http://127.0.0.1:9004")!, responseType: OIDResponseTypeCode, additionalParameters: nil) 

    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    //  appDelegate.currentAuthorizationFlow 
    appDelegate.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self, callback: { (authState, error) in 
     if((authState) != nil){ 
      print("Got authorization tokens. Access token: \(authState?.lastTokenResponse?.accessToken)") 
     }else{ 
      print("Authorization error \(error?.localizedDescription)") 
     } 
    }) 
+0

你設置clientSecret與請求 – Govaadiyo

+0

啊,不。它在文檔「https:// github.com/openid/AppAuth-iOS」中沒有提及如何做到這一點? –

+0

在文檔中找到它,但'client_secret \t從API控制檯獲取的客戶端密鑰。對於註冊爲Android,iOS或Chrome應用程序的客戶端,此值不是必需的,因爲即時消息在iOS中使用它不是必需的嗎? –

回答

0

處理錯誤和更改後我處理redirectUri和令牌後想通了這個問題。 redirectUri - 一旦您使用谷歌授權,它將生成令牌,之後,您應該打開應用程序。 redirectUri將爲此提供幫助。 這就是你可以設置redirectUri

The value for iOS URL scheme wil be the scheme of your redirect URI. This is the Client ID in reverse domain name notation, e.g. com.googleusercontent.apps.IDENTIFIER. To construct the redirect URI, add your own path component. E.g. com.googleusercontent.apps.IDENTIFIER:/oauth2redirect/google. Note that there is only a single slash (/) after the scheme.