[GH-ISSUE #707] url scheme no works,debug out #459

Open
opened 2026-03-03 16:48:49 +03:00 by kerem · 2 comments
Owner

Originally created by @oshju on GitHub (May 17, 2023).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/707

Description:

same as title ,add my code

`import SwiftUI
import Alamofire
import OAuthSwift
import SafariServices

class OAuthHandler: NSObject, OAuthSwiftURLHandlerType {
weak var viewController: UIViewController?

func handle(_ url: URL) {
    guard let viewController = viewController else {
        return
    }
    let safariViewController = SFSafariViewController(url: url)
    viewController.present(safariViewController, animated: true, completion: nil)
}

}

class ViewModel: ObservableObject {
@Published var accessToken: String = ""

func login() {
    let oauthswift = OAuth2Swift(
        consumerKey: "",
        consumerSecret: "",
        authorizeUrl: "https://www.bungie.net/en/oauth/authorize",
        accessTokenUrl: "/platform/app/oauth/token/",
        responseType: "code"
    )

    let handler = OAuthHandler()
    oauthswift.authorizeURLHandler = handler
    handler.viewController = UIApplication.shared.windows.first?.rootViewController

    let state = generateState(withLength: 20) // Genera un estado único para cada solicitud de autorización

    let parameters: [String: Any] = [
        "client_id": "cliient number" // Agrega tu API Key aquí
    ]

    oauthswift.authorize(
        withCallbackURL: URL(string: "com.example.ui:/")!,
        scope: "",
        state: state,
        parameters: parameters,
        completionHandler: { result in
            switch result {
            case .success(let (credential, _, _)):
                self.accessToken = credential.oauthToken
                print("Access token: \(credential.oauthToken)")
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    )
}

// Resto del código de ViewModel...

}

struct ContentView: View {
@StateObject var viewModel = ViewModel()

var body: some View {
VStack {
Text("Access token: (viewModel.accessToken)")
Button("Login") {
viewModel.login()
}

   }

}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// Resto del código de ContentView...
`

OAuth Provider? (Twitter, Github, ..):

Bungie

OAuth Version:

  • Version 1
  • [x ] Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Swift Package Manager
  • Manually

Library version:

  • head
  • [ x] v2.1.0
  • v2.0.0
  • v1.4.1
  • other: (Please fill in the version you are using.)

Xcode version:

  • 11.4 (Swift 5.2)

  • 11.x (Swift 5.1)

  • [ x] 10.x (Swift 5.0)

  • other: (Please fill in the version you are using.)

  • objective c

Originally created by @oshju on GitHub (May 17, 2023). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/707 ### Description: same as title ,add my code `import SwiftUI import Alamofire import OAuthSwift import SafariServices class OAuthHandler: NSObject, OAuthSwiftURLHandlerType { weak var viewController: UIViewController? func handle(_ url: URL) { guard let viewController = viewController else { return } let safariViewController = SFSafariViewController(url: url) viewController.present(safariViewController, animated: true, completion: nil) } } class ViewModel: ObservableObject { @Published var accessToken: String = "" func login() { let oauthswift = OAuth2Swift( consumerKey: "", consumerSecret: "", authorizeUrl: "https://www.bungie.net/en/oauth/authorize", accessTokenUrl: "/platform/app/oauth/token/", responseType: "code" ) let handler = OAuthHandler() oauthswift.authorizeURLHandler = handler handler.viewController = UIApplication.shared.windows.first?.rootViewController let state = generateState(withLength: 20) // Genera un estado único para cada solicitud de autorización let parameters: [String: Any] = [ "client_id": "cliient number" // Agrega tu API Key aquí ] oauthswift.authorize( withCallbackURL: URL(string: "com.example.ui:/")!, scope: "", state: state, parameters: parameters, completionHandler: { result in switch result { case .success(let (credential, _, _)): self.accessToken = credential.oauthToken print("Access token: \(credential.oauthToken)") case .failure(let error): print(error.localizedDescription) } } ) } // Resto del código de ViewModel... } struct ContentView: View { @StateObject var viewModel = ViewModel() var body: some View { VStack { Text("Access token: \(viewModel.accessToken)") Button("Login") { viewModel.login() } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } // Resto del código de ContentView... ` ### OAuth Provider? (Twitter, Github, ..): Bungie ### OAuth Version: - [ ] Version 1 - [x ] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [ ] CocoaPods - [ ] Swift Package Manager - [ ] Manually ### Library version: - [ ] head - [ x] v2.1.0 - [ ] v2.0.0 - [ ] v1.4.1 - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [ ] 11.4 (Swift 5.2) - [ ] 11.x (Swift 5.1) - [ `x]` 10.x (Swift 5.0) - [ ] other: (Please fill in the version you are using.) - [ ] objective c
Author
Owner

@phimage commented on GitHub (May 17, 2023):

  • "noworks" is a bad bug description
  • then learn markdown
  • and do not do let oauthswift -> keep a ref on it (as class attribute or something else)
  • url scheme? you do not show us your url scheme conf, did you follow the doc
  • accessTokenUrl is bad
<!-- gh-comment-id:1551344487 --> @phimage commented on GitHub (May 17, 2023): - "noworks" is a bad bug description - then learn markdown - and do not do `let oauthswift` -> keep a ref on it (as class attribute or something else) - url scheme? you do not show us your url scheme conf, did you follow the doc - accessTokenUrl is bad
Author
Owner

@oshju commented on GitHub (May 18, 2023):

whern debug my code in login funtion,debug mode drops when authorize my application,drops whithout any error message from server or code,my scheme url is com.exmaple.ui,Thanks

<!-- gh-comment-id:1552906788 --> @oshju commented on GitHub (May 18, 2023): whern debug my code in login funtion,debug mode drops when authorize my application,drops whithout any error message from server or code,my scheme url is com.exmaple.ui,Thanks
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#459
No description provided.