With the now.gg SDK for Cocos, you can implement now.gg login within your app/game on the Cocos platform.
To integrate now.gg Login:
Note: Your project’s minimum Android SDK version must not be lower than version 19.
The now.gg SDK for Cocos is included within the download package (nowgg_sdk_cocos-1.2.3.2004.zip
).
To add now.gg SDK within your cocos project:
Once the now.gg SDK has been imported you should now add the required dependencies to your project.
if(ANDROID)
section of your CMakeLists.txt file
list(APPEND GAME_HEADER proj.android/app/jni/nowggsdk/include/nowgg_sdk.h )
if(ANDROID)
section of your CMakeLists.txt file to add nowggsdk to your project.
add_library(nowggsdk STATIC IMPORTED) set_target_properties(nowggsdk PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/proj.android/app/jni/nowggsdk/${ANDROID_ABI}/libnowggsdk.a) target_link_libraries(${APP_NAME} nowggsdk)
gradle.properties
file located in proj.android directory:
android.useAndroidX=true
HelloWorldScene.h
class by navigating to cocosSDKsample > classes directory, within the download package.
#include "../proj.android/app/jni/nowggsdk/include/nowgg_sdk.h"
class HelloWorld : public cocos2d::Scene, public nowgg::NowGGSDKListener
HelloWorldScene.h
class:
private: virtual void onLoginSuccess(string email, string token, string hostUrl) override; virtual void onLoginFailed(int code, string errorMsg) override;
Note: Add the definitions of the virtual functions mentioned above, in HelloWorldScene.cpp
.
build.gradle
file located in proj.android > app directory.
dependencies { implementation (files("libs/billingclient.aar")) implementation (files("libs/nowggsdk.aar")) implementation 'androidx.appcompat:appcompat:1.4.1' api 'com.google.code.gson:gson:2.8.6' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0' implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' }
Once now.gg SDK has been imported, and dependencies have been added; you should now implement now.gg Login to your app/game.
To start the flow, call the login function along with your client_id
as illustrated below:
NowGGSDK::login(<client id from nowgg developer portal>);
Responses related to the now.gg Login can be received using OnLoginSuccess
and OnLoginFailed
callback functions, which you have previously added to HelloWorldScene.h
.
After the user successfully signs in, you will get the requested token and user’s email in the OnLoginSuccess
callback function.
void HelloWorld::onLoginSuccess(string email, string token, string hostUrl) { __android_log_print(ANDROID_LOG_DEBUG, "TAG", "HelloWorld::onLoginSuccess called : %s : %s \n", email.c_str(), token.c_str()); VerifyToken(token, "<client-id from nowgg developer portal>"); }
If the login fails, you will receive an error code and an error message in the OnLoginFailed
callback function. You can investigate this error further based on the error message received.
No nowGG account present
If the login fails with an error “No nowGG account present”, you may call the addNowGGAccount
function of NowGGSDK
class to add an account at runtime, as illustrated below.
void HelloWorld::onLoginFailed(int code, string errorMsg) { __android_log_print(ANDROID_LOG_DEBUG, "TAG", "HelloWorld::onLoginFailed called : %d : %s \n", code, errorMsg.c_str()); NowGGSDK::addNowGGAccount(); }
After a successful sign-in, you now have the token
. You can use this token to fetch User Profile Information.
We recommend that you use a backend server to get the User Profile of a signed-in user. However, if your app does not have a backend server, you can use the following section to retrieve user profile information.
You can refer to our sample implementation to get the profile information for a logged-in user, using an on-device API call.
For example, to retrieve the User Profile Information including the userId of a signed-in user, you may call the VerifyToken
function. You will receive the decoded data of the user as a response.
The following code illustrates this implementation:
void HelloWorld::VerifyToken(string token, string client_id) { __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken called : %s \n", token.c_str()); cocos2d::network::HttpRequest * request = new cocos2d::network::HttpRequest(); request - > setUrl("https://now.gg/accounts/oauth2/v1/verify-token"); std::vector < std::string > headers; headers.push_back("Content-Type:application/x-www-form-urlencoded"); request - > setHeaders(headers); request - > setRequestType(cocos2d::network::HttpRequest::Type::POST); request - > setResponseCallback([ & , this](network::HttpClient * sender, network::HttpResponse * response) { if (!response - > isSucceed()) { __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken failed \n"); return; } __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken success \n"); std::vector < char > * buffer = response - > getResponseData(); char * json = (char * ) malloc(buffer - > size() + 1); std::string s2(buffer - > begin(), buffer - > end()); strcpy(json, s2.c_str()); __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken response: %s \n", json); }); string data = "token_type=id_token&token=" + token + "&client_id=<your client-id>"; __android_log_print(ANDROID_LOG_DEBUG, "TAG", "VerifyToken data:%s \n", data.c_str()); request - > setRequestData(data.c_str(), strlen(data.c_str())); cocos2d::network::HttpClient::getInstance() - > send(request); request - > release(); }
Once you have the user details, you should save that information in your app’s database and use it to skip the login process next time the user launches your app.
Note:
userId
.We have also included an SDK Sample project in the download package. You can refer to this project to understand the now.gg login flow.
The SDK sample project is included in the package in cocosSDKSample
directory.
Document Rev. 1.0