Skip to content

Instantly share code, notes, and snippets.

@revant
Created July 11, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revant/8046963eccecc51e6eb6dff64f6fe379 to your computer and use it in GitHub Desktop.
Save revant/8046963eccecc51e6eb6dff64f6fe379 to your computer and use it in GitHub Desktop.
Sample Scribe OAuth2 for Frappe Server
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mntechnique.oauth2authenticator.LoginActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.mntechnique.oauth2authenticator.utils;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.exceptions.OAuthException;
import com.github.scribejava.core.extractors.TokenExtractor;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
/**
* Created by revant on 11/7/17.
*/
public class FrappeApi extends DefaultApi20 {
protected FrappeApi() {
}
private static class InstanceHolder {
private static final FrappeApi INSTANCE = new FrappeApi();
}
public static FrappeApi instance() {
return InstanceHolder.INSTANCE;
}
@Override
public Verb getAccessTokenVerb() {
return Verb.POST;
}
@Override
public String getAccessTokenEndpoint() {
return "https://erp.mntechnique.com/api/method/frappe.integrations.oauth2.get_token";
}
@Override
protected String getAuthorizationBaseUrl() {
return "https://erp.mntechnique.com/api/method/frappe.integrations.oauth2.authorize";
}
@Override
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
//return OAuth2AccessTokenExtractor.instance();
return new TokenExtractor<OAuth2AccessToken>() {
@Override
public OAuth2AccessToken extract(Response response) throws IOException, OAuthException {
String token = null;
try {
JSONObject bearerToken = new JSONObject(response.getBody());
token = bearerToken.getString("access_token");
} catch (JSONException e) {
e.printStackTrace();
}
return new OAuth2AccessToken(token);
}
};
}
}
package com.mntechnique.oauth2authenticator.utils;
import android.content.Context;
import com.codepath.oauth.OAuthBaseClient;
import com.github.scribejava.core.builder.api.BaseApi;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
* Created by revant on 11/7/17.
*/
public class FrappeClient extends OAuthBaseClient {
public static final BaseApi REST_API_INSTANCE = FrappeApi.instance();
public static final String REST_URL = "https://erp.mntechnique.com/api";
public static final String REST_CONSUMER_KEY = "a0544d8eee";
public static final String REST_CONSUMER_SECRET = "247e2e069c";
public static final String REST_CALLBACK_URL = "oauth2://frappeclient";
public FrappeClient(Context context) {
super(context, REST_API_INSTANCE, REST_URL,
REST_CONSUMER_KEY, REST_CONSUMER_SECRET, REST_CALLBACK_URL);
}
// ENDPOINTS BELOW
public void getHomeTimeline(int page, AsyncHttpResponseHandler handler) {
String apiUrl = getApiUrl("method/frappe.integrations.oauth2.openid_profile");
RequestParams params = new RequestParams();
params.put("page", String.valueOf(page));
client.get(apiUrl, params, handler);
}
}
package com.mntechnique.oauth2authenticator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.codepath.oauth.OAuthLoginActivity;
import com.mntechnique.oauth2authenticator.utils.FrappeClient;
public class LoginActivity extends OAuthLoginActivity<FrappeClient> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getClient().connect();
}
});
}
@Override
public void onLoginSuccess() {
Toast.makeText(getBaseContext(), "Logged In!", Toast.LENGTH_LONG).show();
}
// Fires if the authentication process fails for any reason.
@Override
public void onLoginFailure(Exception e) {
Toast.makeText(getBaseContext(), "ERROR", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment