OAuthSwift的自定义标题

时间:2018-04-12 23:39:32

标签: swift oauth oauth-2.0

我正在使用OAuthSwift对用户进行身份验证,但我已将用户的凭据存储在Cookie中,我需要在尝试授权之前将cookie设置为OAuthSwift实例的标头。

我知道你可以更改OAuthSwiftCredential.authorizationHeader但是添加我自己的标题集呢?这甚至可能吗?

oauth = OAuth2Swift(consumerKey: ""...)

// Change oauth headers here>

oauth.authorize(withCallbackURL: ""...)

2 个答案:

答案 0 :(得分:2)

/ 1.构建授权标头字符串

let auth = OAuthSwiftClient.authorizationHeaderForMethod(method.rawValue,
    url: URLWithPath,
    parameters: parameters!,
    credential: Semantics3Router.oauthClient.credential)

// 2.发出网址请求

let URLRequest = OAuthRequestBuilder.makeRequest(URLWithPath,
    method: method.rawValue,
    headers: [ "Authorization" : auth ],
    parameters: parameters!,
    dataEncoding: dataEncoding)

答案 1 :(得分:0)

结果标头实际上内置在authorize函数中作为可选项。直到我查看源代码时才发现它。

open func authorize(withCallbackURL callbackURL: URL, scope: String, 
    state: String, parameters: Parameters = [:], headers: 
    OAuthSwift.Headers? = nil, success: @escaping TokenSuccessHandler, 
    failure: FailureHandler?) -> OAuthSwiftRequestHandle?

Xcode用于显示自动填充选项的选项,我想知道为什么它不再这样做了?