i need some help trying to implement a 3 legged Oauth2 to google on google app engine using golang.
I have been following this workflow https://developers.google.com/accounts/docs/CrossClientAuth
I'm also using the github.com/golang/oauth2 package for oauth2 golang authentication.
in the google documentation it says that i need to get a code from the front-end client send it to the backend and exchange the code to a valid Token.
android:
//service account id
private final String scopes = "audience:server:client_id:XXX-XXX"
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN).build();
findViewById(R.id.log).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LongOperation longOperation = new LongOperation();
//calling the async to perform the service call
longOperation.execute(scopes);
}
});
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
code = GoogleAuthUtil.getToken(MainActivity.this,Plus.AccountApi.getAccountName(mGoogleApiClient),params[0]);
} catch (GoogleAuthException e) {
e.printStackTrace();
Log.d("ok",String.valueOf(e));
} catch (IOException e) {
e.printStackTrace();
Log.d("ok",String.valueOf(e));
}
Log.d("ok",String.valueOf(code));
return null;
}
}
NOW : backend ..
i have created a GET endpoint (for now .. it will be a POST) in order to pass the code that I'm getting in the android async trying to exchange it to a valid token by calling google Oauth2 supplying it with ServiceAccounts id using JSON. i follow this documentation http://godoc.org/github.com/golang/oauth2/google ->> ServiceAccountJSON.
package Oauth2
import (
"github.com/golang/oauth2"
"net/http"
"github.com/golang/oauth2/google"
"appengine/urlfetch"
"appengine"
)
func ExchangeCode(r *http.Request,code string){
c := appengine.NewContext(r);
//setting endpoints for calling the get supplying android client code , and oauth2callback
oauth2.Endpoint("https://XXX.appspot.com/oauth2","https://XXX.appspot.com/oauth2callback");
f, err := oauth2.New(
google.ServiceAccountJSONKey("static/XXX.json"), //reading the json file that was supplyde from the google console
oauth2.Scope("https://www.googleapis.com/auth/userinfo.profile"),
)
if err != nil {
c.Infof(err.Error());
}
f.AuthCodeURL("tiqitiq", "offline", "auto"); //u := *f.opts.AuthURL
c.Infof("flow v%",f);
client := urlfetch.Client(c);
t,err := f.NewTransportFromCode(code);
if err != nil {
c.Errorf(err.Error());
}
client.Transport = t;
c.Infof(err.Error());
return
}
c.Infof("resp:= %v",resp);
}
I don't quite seem to get it right ..
from what i understand i need to call this function and supply params to get the right Token call. but i get nil pointer exception on runtime trying to use this package..
func : f.AuthCodeURL("tiqitiq", "offline", "auto");
Oauth2 package exception : u := *f.opts.AuthURL;
further more i understand that i need to use the t,err := f.NewTransportFromCode(code); function to transport the code got from the android app to the google Oauth2 servers and get a callback giving me the Token and refresh-token-url .. this line also brakes..
this all happening on google app engine using the golang .. please help I'm so stuck ..