Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example not work #1

Open
Frimko opened this issue Jan 9, 2018 · 1 comment
Open

example not work #1

Frimko opened this issue Jan 9, 2018 · 1 comment

Comments

@Frimko
Copy link

Frimko commented Jan 9, 2018

http://joxi.ru/GrqgGdnCQv0e12?d=1

@4external
Copy link

4external commented Feb 21, 2018

I modified code to

`
package com.askiirobotics.reactnativeoksdk;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.support.annotation.NonNull;

import com.facebook.react.bridge.*;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;

import org.json.JSONObject;

import ru.ok.android.sdk.Odnoklassniki;
import ru.ok.android.sdk.OkListener;
import ru.ok.android.sdk.Shared;
import ru.ok.android.sdk.util.OkAuthType;

public class OkManager extends ReactContextBaseJavaModule implements ActivityEventListener {
private static final String LOG = "OkManager";
private static final String E_LOGIN_ERROR = "E_LOGIN_ERROR";
private static final String E_GET_USER_FAILED = "E_GET_USER_FAILED";

private Odnoklassniki odnoklassniki;
private String redirectUri;
private Promise loginPromise;

@Override
public void onNewIntent(Intent intent) {}

public OkManager(final ReactApplicationContext reactContext) {
    super(reactContext);
    reactContext.addActivityEventListener(this);
}

@Override
public String getName() {
    return "OkManager";
}

@ReactMethod
public void initialize(final String appId, final String appKey) {
    Log.d(LOG, "Inititalizing app " + appId + " with key " + appKey);
    odnoklassniki = Odnoklassniki.createInstance(getReactApplicationContext(), appId, appKey);
    redirectUri = "okauth://ok" + appId;
}

@ReactMethod
public void login(final ReadableArray scope, final Promise promise) {
    int scopeSize = scope.size();
    final String[] scopeArray = new String[scopeSize];
    for (int i = 0; i < scopeSize; i++) {
        scopeArray[i] = scope.getString(i);
    }
    loginPromise = promise;
    odnoklassniki.checkValidTokens(new OkListener() {
        @Override
        public void onSuccess(JSONObject json) {
            Log.d(LOG, "Check valid token success");
            resolveWithCurrentUser(json.optString(Shared.PARAM_ACCESS_TOKEN), json.optString(Shared.PARAM_SESSION_SECRET_KEY));
        }

        @Override
        public void onError(String error) {
            Log.d(LOG, "Valid token wasn't found at login, requesting authorization");
            odnoklassniki.requestAuthorization(getCurrentActivity(), redirectUri, OkAuthType.ANY, scopeArray);
        }
    });
}

@ReactMethod
public void logout(Promise promise) {
    Log.d(LOG, "Logout");
    odnoklassniki.clearTokens();
    promise.resolve(null);
}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
//public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (Odnoklassniki.getInstance().isActivityRequestOAuth(requestCode)) {
        Odnoklassniki.getInstance().onAuthActivityResult(requestCode, resultCode, data, getAuthListener());
    }
}

@NonNull
private OkListener getAuthListener() {
    return new OkListener() {
        @Override
        public void onSuccess(final JSONObject json) {
            Log.d(LOG, "Activity auth success");
            resolveWithCurrentUser(json.optString(Shared.PARAM_ACCESS_TOKEN), json.optString(Shared.PARAM_SESSION_SECRET_KEY));
        }

        @Override
        public void onError(String error) {
            Log.d(LOG, "OK Oauth error " + error);
            loginPromise.reject(E_LOGIN_ERROR, error);
        }
    };
}

private void resolveWithCurrentUser(final String accessToken, final String sessionSecretKey){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //String userStr = odnoklassniki.request("users.getCurrentUser", "get");
                String userStr = odnoklassniki.request("users.getCurrentUser",null, "get");
                JSONObject user = new JSONObject(userStr);
                WritableMap result = Arguments.createMap();
                result.putString(Shared.PARAM_ACCESS_TOKEN, accessToken);
                result.putString(Shared.PARAM_SESSION_SECRET_KEY, sessionSecretKey);
                result.putMap("user", JSONUtil.convertMap(user));
                loginPromise.resolve(result);
            } catch (Exception e) {
                loginPromise.reject(E_GET_USER_FAILED, "users.getLoggedInUser failed: " + e.getLocalizedMessage());
            }
        }
    }).start();
}

}
`

`
package com.askiirobotics.reactnativeoksdk;

import com.askiirobotics.reactnativeoksdk.OkManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class OkPackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();

    modules.add(new OkManager(reactContext));
    return modules;
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Arrays.<ViewManager>asList();
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants