swift ZOHO CRM未获得响应中的刷新令牌

jogvjijk  于 2023-06-21  发布在  Swift
关注(0)|答案(3)|浏览(99)

我正在用iOSandroid开发一个应用程序,因为我正在集成ZOHO CRM。我使用OAuth2.0进行身份验证,之后我使用REST API获取“刷新令牌”,但我只获取“访问令牌”。下面有代码来获取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获取刷新和访问令牌。

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
}
gr8qqesn

gr8qqesn1#

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

access_type=offline&prompt=consent

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

wgmfuz8q

wgmfuz8q2#

在生成授权码时使用prompt=consent和access_type=offline。

6yt4nkrj

6yt4nkrj3#

我也面临着同样的问题。我有access_type=offline,但没有prompt=consent
当我添加scopeZohoCRM.users.ALL时,我得到了刷新令牌,这似乎不正确,因为我正在集成Zoho Books而不是Zoho CRM。

相关问题