2017-04-20 10 views
0

私はLaravel Passportアプリケーションを構築しました。Laravel Passportモバイルアプリへのアクセス

私が経路+トークンをPostmanに入れれば、それは完璧に動作し、私はjsonデータを取得します。

アンドロイドアプリケーションがこれらのjsonデータにアクセスする必要がある場合のロジックは次のとおりです。

アンドロイドアプリがログインしてjsonデータにアクセスするためのトークンを取得する必要がありますか?

このプログラムをプログラムで作成するにはどうすればよいですか?

現在のトークンが私がログインしようとする各Androidデバイスのためにこれを自動的に作成するにはどうすればボタンのクリックを介して手動で作成される。

私がこれまでに構築されたアプリケーションにドキュメントhttps://laravel.com/docs/5.4/passport#protecting-routesを追いました。しかし、私の知識はこの時点で終わります。

答えて

0

アンドロイドアプリがログインしてトークンを取得する必要がありますか? jsonデータにアクセスしますか?

はい、それは私がプログラムでこの作業を行うにはどうすればよい

トークンユーザーが必要ですか?

まず、パスポートパッケージを使用してWebサーバーLaravelに「パスワード許可クライアント」を作成する必要があります。

public class SignInRequestModel { 
    public String client_id; 
    public String client_secret; 
    public String username; 
    public String password; 
    public String grant_type="password"; 

    public SignInRequestModel() { 
     this.client_id = Config.CLIENT_ID; 
     this.client_secret = Config.CLIENT_SECRET; 
    } 
} 

とあなたのサービスを生成しますログインサービス

private void login() { 
     SignInRequestModel signInRequestModel = new SignInRequestModel(); 
     signInRequestModel.username = this.email; //User email 
     signInRequestModel.password = this.password; // User Password 
     //provide service 
     ServiceGenerator provider = new ServiceGenerator(); 
     ProjectService tService = provider.getService(); 
     //make call 
     appPreferenceTools = new AppPreferenceTools(LoginActivity.this); 
     Call<AuthenticationResponseModel> call = tService.signIn(signInRequestModel); 
     progressBar.setVisibility(View.VISIBLE); 
     call.enqueue(new Callback<AuthenticationResponseModel>() { 
      @Override 
      public void onResponse(Call<AuthenticationResponseModel> call, Response<AuthenticationResponseModel> response) { 
       if (response.isSuccessful()) { 
        appPreferenceTools.saveUserAuthenticationInfo(response.body()); //save user token 
        Snackbar.make(parent_view, "Login Success", Snackbar.LENGTH_SHORT).show(); 
        // ToDo Your Code 
      } 

      @Override 
      public void onFailure(Call<AuthenticationResponseModel> call, Throwable t) { 
       //occur when fail to deserialize || no network connection || server unavailable 
       Log.d(TAG, t.getMessage() + " " + t.getCause()); 
      } 


     }); 

    } 

ServiceGeneratorクラスを呼び出しますと動作しますloginメソッドを作成します。

今のAndroid側では、あなたのリクエスト情報が含まれているsignInRequestModelを作成する必要がありますヘッダー要求にユーザートークンを追加し、必要に応じてリフレッシュトークンを取得します。

public class ServiceGenerator { 

    private ProjectService mTService; 
    private Retrofit mRetrofitClient; 
    private AppPreferenceTools tools; // PreferenceTools for Your Project 
    private OkHttpClient.Builder httpClient; 

    public ServiceGenerator() { 
     tools = new AppPreferenceTools(App.getContext()); 

     httpClient = new OkHttpClient.Builder(); 

     /* you need */ 
     HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); // for debug 
     logging.setLevel(HttpLoggingInterceptor.Level.BODY); // for debug 
     httpClient.addInterceptor(logging); // for debug 


     httpClient.authenticator(new Authenticator() { 
      @Override 
      public Request authenticate(Route route, Response response) throws IOException { 
       if (tools.isAuthorized()) { 
        //make the refresh token request model 
        RefreshTokenRequestModel requestModel = new RefreshTokenRequestModel(); 
        requestModel.refresh_token = tools.getRefreshToken(); 
        //make call 
        Call<AuthenticationResponseModel> call = mTService.getRefreshModel(requestModel); 
        retrofit2.Response<AuthenticationResponseModel> tokenModelResponse = call.execute(); 
        if (tokenModelResponse.isSuccessful()) { 
         tools.saveUserAuthenticationInfo(tokenModelResponse.body()); 
         System.out.println(tokenModelResponse.body()); 
         return response.request().newBuilder() 
           .removeHeader("Authorization") 
           .addHeader("Authorization", "Bearer " + tools.getAccessToken()) 
           .build(); 
        } else { 
         return null; 
        } 
       } else { 
        return null; 
       } 
      } 
     }); 


     httpClient.addInterceptor(new Interceptor() { 
      @Override 
      public Response intercept(Chain chain) throws IOException { 
       Request orginal = chain.request(); 
       Request.Builder builder = orginal.newBuilder(); 
       builder.addHeader("Accept", "application/json"); 
       if (tools.isAuthorized()) { 
        builder.addHeader("Authorization", "Bearer " + tools.getAccessToken()); 
       } 
       builder.method(orginal.method(), orginal.body()); 
       Request build = builder.build(); 
       return chain.proceed(build); 
      } 
     }); 

     //create new gson object to define custom converter on Date type 
     Gson gson = new GsonBuilder() 
       .create(); 

     mRetrofitClient = new Retrofit.Builder() 
       .baseUrl(Config.AP_URL_BASE) // set Base URL , should end with '/' 
       .client(httpClient.build()) // add http client 
       .addConverterFactory(GsonConverterFactory.create(gson))//add gson converter 
       .build(); 
     mTService = mRetrofitClient.create(ProjectService.class); 

    } 

    public ProjectService getService() { 
     return mTService; 
    } 

    public Retrofit getmRetrofitClient() { 
     return mRetrofitClient; 
    } 

} 

最後に、あなたのルートのためのインターフェイスを作成する必要があります:あなたが必要となります

public interface ProjectService { 
    @POST("oauth/token") 
    Call<AuthenticationResponseModel> signIn(@Body SignInRequestModel signInRequestModel); 
// Todo Refresh Token ,sign Up 

} 

パッケージ:答えを

compile 'com.squareup.retrofit2:retrofit:2.1.0' 
    compile 'com.squareup.retrofit2:converter-gson:2.1.0' 
    compile 'com.squareup.okhttp3:logging-interceptor:3.5.0' 
+0

おかげで現在、私はこれをテストする時間がない、私はおそらく約10日間であなたに返信されます。 – utdev

関連する問題