如何在JAVA中使用Gigya API实现登录功能?

时间:2015-05-06 20:01:58

标签: java gigya

我正在尝试从Gigya DB中删除帐户,因此我们可以重复使用它们来通过Gigya测试我们的登录功能。似乎删除所需的UID来自登录,所以我怎么想用Java做呢?

1 个答案:

答案 0 :(得分:0)

正如Ilan所说,首先你需要包含Gigya Java SDK。

然后,您可以使用Gigya控制台中的身份访问或身份查询工具查找UID,并使用以下代码删除该帐户:

// delete user record
GSRequest deleteAccountRequest = new GSRequest(apiKey, secretKey, "accounts.deleteAccount");
//deleteAccountRequest.setAPIDomain("eu1.gigya.com");  // enable this if you're using the EU data centre
deleteAccountRequest.setUseHTTPS(true);
deleteAccountRequest.setParam("UID", uid);
GSResponse deleteAccountResponse = deleteAccountRequest.send();
if(deleteAccountResponse.getErrorCode()==0)
{     
} 
else 
{  
    System.out.println("deleteAccountResponse failure: " + deleteAccountResponse.getLog());
}

或者,如果要批量删除用户,可以使用accounts.search运行搜索并删除结果集中的所有用户:

int limit = 100;
String query = "select UID from accounts where ... " + limit; // add your query here i.e. email = 'someone@example.com'
String cursorId = ""; 
int objectsCount = limit;

GSRequest searchRequest;
ArrayList<String> uidList = new ArrayList<String>();

// send request
do 
{
    // check if we have an open cursor
    if(cursorId.length() > 0)
    {
        // run next request in cursor

        // set up request
        searchRequest = new GSRequest(apiKey, secretKey, "accounts.search");
        //searchRequest.setAPIDomain("eu1.gigya.com");
        //searchRequest.setUseHTTPS(true);

        // set timeout
        searchRequest.setParam("timeout", 60000);

        // set cursor id
        searchRequest.setParam("cursorId", cursorId);
    } else {
        // run new request and open cursor

        // set up request
        searchRequest = new GSRequest(apiKey, secretKey, "accounts.search");
        //searchRequest.setAPIDomain("eu1.gigya.com");
        //searchRequest.setUseHTTPS(true);

        // set timeout
        searchRequest.setParam("timeout", 60000);

        // set query
        searchRequest.setParam("query", query);

        // open cursor
        searchRequest.setParam("openCursor", true);
    }

    GSResponse searchResponse = searchRequest.send();
    if(searchResponse.getErrorCode()==0)
    {     
        GSArray uids = new GSArray();
        uids = searchResponse.getArray("results", uids);
        for(int i=0; i<uids.length(); i++)
        {
            String uid;
            try {
                // retrieve uid and add to list of uids
                uid = uids.getObject(i).getString("UID");
                uidList.add(uid);

            } catch (GSKeyNotFoundException e) {
            }

        }

        cursorId = searchResponse.getString("nextCursorId", "");
        objectsCount = searchResponse.getInt("objectsCount", 0);
    } 
    else 
    {  
        System.out.println("searchRequest failure: " + searchResponse.getLog());
    } 
} 
while (objectsCount >= limit);

for(int i=0; i<uidList.size(); i++)
{
    String uid;
    try {
        uid = uidList.get(i);

        // delete user record
        GSRequest deleteAccountRequest = new GSRequest(apiKey, secretKey, "accounts.deleteAccount");
        //deleteAccountRequest.setAPIDomain("eu1.gigya.com");
        deleteAccountRequest.setUseHTTPS(true);
        deleteAccountRequest.setParam("UID", uid);
        GSResponse deleteAccountResponse = deleteAccountRequest.send();
        if(deleteAccountResponse.getErrorCode()==0)
        {     
        } 
        else 
        {  
            System.out.println("deleteAccountResponse failure: " + deleteAccountResponse.getLog());
        }

    } catch (Exception e) {
    }

}