Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
adrielcafe committed Jul 12, 2016
1 parent d73e131 commit 4a13204
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 26 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ I've made this lib in a hurry to my startup. My plan is to improve over time and

### Google
```java
// Use a Browser credential instead of Android credential
// Use a Web credential instead of Android credential
GoogleOAuth.Builder(this)
.setClientId(Credentials.GOOGLE_CLIENT_ID)
.setClientSecret(Credentials.GOOGLE_CLIENT_SECRET)
.setRedirectUri(Credentials.GOOGLE_REDIRECT_URI)
.setTokenCallback(new OnGetTokenCallback() {
@Override
public void onSuccess(String token, SocialUser user) {
Expand Down Expand Up @@ -61,7 +62,7 @@ repositories {
}
dependencies {
compile 'com.github.adrielcafe:AndroidOAuth:1.0.0'
compile 'com.github.adrielcafe:AndroidOAuth:1.0.1'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Credentials {
public static final String GOOGLE_CLIENT_ID = "REPLACE";
public static final String GOOGLE_CLIENT_SECRET = "REPLACE";
public static final String GOOGLE_REDIRECT_URI = "REPLACE";

public static final String FACEBOOK_APP_ID = "REPLACE";
public static final String FACEBOOK_APP_SECRET = "REPLACE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void googleLogin(View v){
GoogleOAuth.Builder(this)
.setClientId(Credentials.GOOGLE_CLIENT_ID)
.setClientSecret(Credentials.GOOGLE_CLIENT_SECRET)
.setRedirectUri(Credentials.GOOGLE_REDIRECT_URI)
.setTokenCallback(new OnGetTokenCallback() {
@Override
public void onSuccess(String token, SocialUser user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,16 @@

public final class GoogleOAuth extends BaseOAuth {
private static final String SCOPE = "profile email";
private static final String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob:auto";
private static final String GET_ACCOUNT_URL = "https://www.googleapis.com/plus/v1/people/me";

private GoogleOAuth(Activity activity){
super(activity, GoogleApi20.instance(), SCOPE, GET_ACCOUNT_URL);
redirectUri = REDIRECT_URI;
}

public static GoogleOAuth Builder(Activity activity){
return new GoogleOAuth(activity);
}

@Override
public BaseOAuth setRedirectUri(String callback){
throw new IllegalArgumentException("Must use the default Google URI redirect");
}

@Override
protected SocialUser toAccount(String json){
try {
Expand Down
24 changes: 7 additions & 17 deletions lib/src/main/java/cafe/adriel/androidoauth/view/ConsentDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
Expand Down Expand Up @@ -49,8 +50,8 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(view.getTitle().startsWith("Success") || url.contains("code=")) {
getCode(view.getTitle(), url);
if(url.contains("code=") && url.contains("state=")) {
getCode(url);
}
}
});
Expand All @@ -75,21 +76,10 @@ public ConsentDialog setOnGetCodeCallback(OnGetCodeCallback callback){
return this;
}

private void getCode(String title, String url){
String receivedState = "";
if(title.startsWith("Success")){
receivedState = title.substring(title.indexOf("state="), title.indexOf("&"))
.replace("state=", "");
code = title.substring(title.indexOf("code="))
.replace("code=", "");
} else if(url.contains("code=") && url.contains("state=")){
receivedState = url.substring(url.indexOf("state="))
.replace("state=", "")
.replace("#", "")
.replace("_=_", "");
code = url.substring(url.indexOf("code="), url.indexOf("&"))
.replace("code=", "");
}
private void getCode(String url){
Uri uri = Uri.parse(url);
String receivedState = uri.getQueryParameter("state");
code = uri.getQueryParameter("code");
if(code != null && !code.isEmpty() && originalState.equals(receivedState)){
callback.onSuccess(code);
} else {
Expand Down

0 comments on commit 4a13204

Please sign in to comment.