【问题标题】:Storing Access token, Google+ integration Android App存储访问令牌,Google+ 集成 Android App
【发布时间】:2014-07-23 17:36:31
【问题描述】:

我是 android 的新手,我开始从 android SDK 中的 GooglePlayServices 导入示例应用程序。 应用“Plus”允许我使用 G+ 凭据登录。示例代码使用 GoogleApiClient

这仅在客户端验证应用程序。如何将访问令牌发送到我的服务器或将访问令牌存储到我的共享首选项??

有人请解释我应该将访问令牌代码添加到哪个文件。?

【问题讨论】:

  • 你收到AccessToken了吗?
  • 不,我必须在后台线程中请求它。你能帮我做这件事并最终存储它吗?

标签: android login google-plus access-token google-api-client


【解决方案1】:

我假设您能够成功登录 GooglePlus。执行以下操作以获取 AccessToken 并使用 SharedPreference 存储它,

SharedPreferences SharedPreference;    
Editor editor;

    private class GetGoogleAuthTask extends AsyncTask<Void, Void, String>  {
             @Override
             protected String doInBackground(Void... params) {
                 String token = null;

                 try {
                     token=GoogleAuthUtil.getToken(User_SignUp.this,Plus.AccountApi.getAccountName(mGoogleApiClient),"oauth2:"+Scopes.PLUS_LOGIN+" "+Scopes.PLUS_ME); //Change the permissions as per your need.
                 } catch (IOException transientEx) {
                     // Network or server error, try later
                     Log.e(TAG, transientEx.toString());
                 } catch (UserRecoverableAuthException e) {
                     // Recover (with e.getIntent())
                     Log.e(TAG, e.toString());
                    // Intent recover = e.getIntent();
                    // startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
                 } catch (GoogleAuthException authEx) {
                     // The call is not ever expected to succeed
                     // assuming you have already verified that 
                     // Google Play services is installed.
                     Log.e(TAG, authEx.toString());
                 }

                 return token;
             }

             @Override
             protected void onPostExecute(String token) {

                 if(token!=null)
                 {
                  Log.i(TAG, "Access token retrieved:" + token);
                  SharedPreference = getApplicationContext().getSharedPreferences("TokenPreference", 0); 
                  editor = SharedPreference.edit();
                  editor.putString("access_token",token);                               
                  editor.commit();                      

                 }

             }

         }

在需要获取accesstoken的地方使用sn-p,

new GetGoogleAuthTask().execute();

【讨论】:

  • 你能告诉我.. 我如何将这个获得的令牌发送到我的服务器?
  • 您只需要像简单的字符串数据一样发送它。阅读一些关于如何使用 HTTP 将数据发送到服务器的教程。
  • 我知道..!但在java中我想要一些实现演示。我实际上有一个节点服务器正在运行。现在我想至少在我的控制台中输出这个获得的令牌。在 tokenValue=new GetGoogleAuthTask().execute() 下面我需要将它发送到服务器吗?
  • 我还想问一件事。我需要编写代码来验证服务器上的访问令牌。那么如何做到这一点呢?我的服务器代码可能不在 java 中。对吗?
猜你喜欢
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 2012-07-18
  • 2021-07-16
  • 2021-09-13
  • 2021-10-29
相关资源
最近更新 更多