ZOHO CRM未获得刷新令牌作为响应

问题描述 投票:0回答:1

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

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的代码以获取刷新和访问令牌后。

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()
}

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

{
    "access_token": "1000.2......",
    "expires_in_sec": 3600,
    "api_domain": "https://www.zohoapis.in",
    "token_type": "Bearer",
    "expires_in": 3600000
}
swift oauth-2.0 crm zoho refresh-token
1个回答
2
投票

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

access_type=offline&prompt=consent

这可确保您获得刷新令牌以及访问令牌。

© www.soinside.com 2019 - 2024. All rights reserved.