swift - ZOHO CRM 未获得刷新 token 作为响应

标签 swift oauth-2.0 crm zoho refresh-token

我正在 iOSandroid 中开发应用程序,因为我正在集成 ZOHO CRM。我使用 OAuth2.0 进行身份验证,之后我使用 REST API 来获取“刷新 token ”,但我只得到“访问 token ”。有下面的代码来获取 token 。如何获取刷新 token ?

self.getCodeFromCRM(client_id: Client_ID,
                            clientSecret: secID,
                            authURL: "https://accounts.zoho.in/oauth/v2/auth",
                            accessURL: "offline",
                            responseType: "code",
                            callBackURL: "zohoapp://",
                            scope: "ZohoCRM.modules.contacts.all",//ZohoCRM.users.ALL
                            state: "code")

获取代码后调用此 API 获取刷新和访问 token 。

func getZohoReferenceToken()
    {
        let headers = [
            "Content-Type": "application/x-www-form-urlencoded",
            "User-Agent": "PostmanRuntime/7.13.0",
            "Accept": "*/*",
            "Cache-Control": "no-cache",
            "Postman-Token": "88ebde59-240a-4e52-8ff9-bb7384eba0dd,9a1d5ea1-a5c0-490e-b3b5-1884e335ef86",
            "Host": "accounts.zoho.in",
            "accept-encoding": "gzip, deflate",
            "content-length": "254",
            "Connection": "keep-alive",
            "cache-control": "no-cache"
        ]

        let postData = NSMutableData(data: "client_id=\(Client_ID)".data(using: String.Encoding.utf8)!)
        postData.append("&client_secret=\(secID)".data(using: String.Encoding.utf8)!)
        postData.append("&redirect_uri=zohoapp://".data(using: String.Encoding.utf8)!)
        postData.append("&code=\(code)".data(using: String.Encoding.utf8)!)
        postData.append("&grant_type=authorization_code".data(using: String.Encoding.utf8)!)
        postData.append("&prompt=consent".data(using: String.Encoding.utf8)!)

        let request = NSMutableURLRequest(url: NSURL(string: "https://accounts.zoho.in/oauth/v2/token")! as URL,
                                          cachePolicy: .useProtocolCachePolicy,
                                          timeoutInterval: 10.0)
        request.httpMethod = "POST"
        request.allHTTPHeaderFields = headers
        request.httpBody = postData as Data

        let session = URLSession.shared
        let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            if (error != nil) {
                print(error!)
            } else {
                let httpResponse = response as? HTTPURLResponse
                print(httpResponse!)

                do {
                    //create json object from data
                    if let json:NSDictionary = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                    {
                        // UserDefaults.standard.set(json.value(forKey: "access_token") as! String, forKey: "ZOHO_access")
                        print(json)
                        let access:String = ""//json.value(forKey: "access_token") as! String;
                        let ref:String = ""//json.value(forKey: "refresh_token") as! String

                        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0, execute: {
                            self.displayAlert(appname: "ZOHO", accessToken: access, referenseToken: ref)
                        })
                    }
                } catch let error {
                    print(error.localizedDescription)
                }
            }
        })

        dataTask.resume()
}

响应:您可以在下面的响应中看到我没有获得刷新 token 。请帮助我如何获得刷新 token ?

{
    "access_token": "1000.2......",
    "expires_in_sec": 3600,
    "api_domain": "https://www.zohoapis.in",
    "token_type": "Bearer",
    "expires_in": 3600000
}

最佳答案

我遇到了同样的问题,但最终在 Mail API 的文档中找到了答案。要强制刷新 token ,您需要向初始 Post 添加两个附加参数:

access_type=offline&prompt=consent

这可确保您获得刷新 token 和访问 token 。

关于swift - ZOHO CRM 未获得刷新 token 作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57035108/

相关文章:

ios - 在 iOS 中使用导航的私有(private)属性(property)离线 map

oauth-2.0 - 如何防止刷新被盗的访问 token

oauth - Youtube OAuth 2.0 用户 token 有效期是多少?

dynamics-crm - 导航工具栏上的 Dynamics Crm 365 自定义按钮

version-control - 使用 Siebel CRM 的推荐开发实践?

ios - Swift:以动态方式垂直对齐父 View 中心的 subview ?使用人工计算?最好使用 SKSpriteNodes?

ios - 如何声明也符合协议(protocol)的特定类的属性?

javascript - 在 ajax 查询中将 async 属性设置为 false 时,不会调用 Web Api Controller

ios - Xcode 12 模块 'RxSwift' 未在库演化支持下编译;使用它意味着无法保证 '' 的二进制兼容性

javascript - Google OAuth2 - 交换 token 的访问代码 - 不起作用