我需要一些帮助尝试使用golang在谷歌应用引擎上实现一个3腿的Oauth2 google .

我一直在关注这个工作流程https://developers.google.com/accounts/docs/CrossClientAuth

我也使用github.com/golang/oauth2包进行oauth2 golang身份验证 .

在谷歌文档中,它说我需要从前端客户端获取代码将其发送到后端并将代码交换为有效的令牌 .

机器人:

//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;
        }
    }

现在:后端..

我创建了一个GET endpoints (现在..它将是一个POST),以便传递我在android异步中获取的代码,尝试通过调用google Oauth2将其交换为有效令牌,并使用ServiceAccounts id为其提供JSON . 我按照此文档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);
}

我似乎没有把它弄好..

根据我的理解,我需要调用此函数并提供params以获得正确的令牌调用 . 但我在运行时试图使用此包得到nil指针异常..

func : f.AuthCodeURL("tiqitiq", "offline", "auto"); 
Oauth2 package exception : u := *f.opts.AuthURL;

我还明白我需要使用t,错误:= f.NewTransportFromCode(code);功能将从Android应用程序获取的代码传输到谷歌Oauth2服务器并获得回调给我令牌和刷新令牌网址..此行也刹车..

这一切都发生在谷歌应用程序引擎使用golang ..请帮助我这么卡住..