Dribbble Android誓言

时间:2018-07-15 14:47:05

标签: android oauth

当我尝试在应用程序中进行宣誓登录时,出现以下错误: error message 这是Auth.java:

REQ_CODE = 100;
KEY_CODE = "code";
KEY_CLIENT_ID = "client_id";
KEY_CLIENT_SECRET = "client_secret";
KEY_REDIRECT_URI = "redirect_uri";
KEY_SCOPE = "scope";
KEY_ACCESS_TOKEN = "access_token";
CLIENT_ID = "c635cc277303e7bc9d553c98c1586568b5063044171adfab77fe078ebcc6ee83";
private static final String CLIENT_SECRET = "c18308b4adf98febefbcecf934012bb49ab6f2f41dac1f148600d962216e39a6";
private static final String SCOPE = "public+write";
private static final String URI_AUTHORIZE = "https://dribbble.com/oauth/authorize";
private static final String URI_TOKEN = "https://dribbble.com/oauth/token";
public static final String REDIRECT_URI = "http://www.google.com";
private static String getAuthorizeUrl() {
    String url = Uri.parse(URI_AUTHORIZE)
            .buildUpon()
            .appendQueryParameter(KEY_CLIENT_ID, CLIENT_ID)
            .build()
            .toString();
    // fix encode issue
    url += "&" + KEY_REDIRECT_URI + "=" + REDIRECT_URI;
    url += "&" + KEY_SCOPE + "=" + SCOPE;
    return url;
}
private static String getTokenUrl(String authCode) {
    return Uri.parse(URI_TOKEN)
            .buildUpon()
            .appendQueryParameter(KEY_CLIENT_ID, CLIENT_ID)
            .appendQueryParameter(KEY_CLIENT_SECRET, CLIENT_SECRET)
            .appendQueryParameter(KEY_CODE, authCode)
            .appendQueryParameter(KEY_REDIRECT_URI, REDIRECT_URI)
            .build()
            .toString();
}
public static void openAuthActivity(@NonNull Activity activity) {
    Intent intent = new Intent(activity, AuthActivity.class);
    intent.putExtra(AuthActivity.KEY_URL, getAuthorizeUrl());
    activity.startActivityForResult(intent, REQ_CODE);
}
public static String fetchAccessToken(String authCode) throws IOException {
    OkHttpClient client = new OkHttpClient();
    RequestBody postBody = new FormBody.Builder()
            .add(KEY_CLIENT_ID, CLIENT_ID)
            .add(KEY_CLIENT_SECRET, CLIENT_SECRET)
            .add(KEY_CODE, authCode)
            .add(KEY_REDIRECT_URI, REDIRECT_URI)
            .build();
    Request request = new Request.Builder()
            .url(URI_TOKEN)
            .post(postBody)
            .build();
    Response response = client.newCall(request).execute();

    String responseString = response.body().string();
    try {
        JSONObject obj = new JSONObject(responseString);
        return obj.getString(KEY_ACCESS_TOKEN);
    } catch (JSONException e) {
        e.printStackTrace();
        return "";
        }
    }
}

和LoginActivity.java:

public class LoginActivity extends AppCompatActivity {

@BindView(R.id.activity_login_btn) TextView loginBtn;

@Override
protected void onResume() {
    super.onResume();
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    ButterKnife.bind(this);

    // load access token from shared preference
    Dribbble.init(this);

    if (!Dribbble.isLoggedIn()) {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Auth.openAuthActivity(LoginActivity.this);
            }
        });
    } else {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Auth.REQ_CODE && resultCode == RESULT_OK) {
        final String authCode = data.getStringExtra(AuthActivity.KEY_CODE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // this is a network call and it's time consuming
                    // that's why we're doing this in a non-UI thread
                    String token = Auth.fetchAccessToken(authCode);

                    // store access token in SharedPreferences
                    Dribbble.login(LoginActivity.this, token);

                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } catch (IOException | JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
}

任何帮助将不胜感激。当打开应用程序时,它打开LoginActivity,然后跳到webveiw。单击登录后,应用程序崩溃,无法跳至LoginActivity和MainActivity。 Dribbble.init将访问令牌。

1 个答案:

答案 0 :(得分:0)

问题似乎出在网址中。我建议您一次查找网址。您必须重定向到在应用程序开发人员中创建的相同URL。