How to create a parse _User account from a Android Google token?

前端 未结 2 1407
滥情空心
滥情空心 2021-01-05 13:40

I\'ve found some fragments of useful information.

http://blog.parse.com/announcements/bring-your-own-login/ shows me how to login an Android app once I have

2条回答
  •  情深已故
    2021-01-05 13:48

    Steps:

    1. Get the Google token
    2. Send the Google token to your Parse back-end to validate
    3. Use the validated Google token to create a Parse session token
    4. Use the Parse session token to allow users to access the app

    (1): See the guide: https://developers.google.com/identity/sign-in/android/sign-in


    (2): Send the ID token to your back-end server (in this case Parse).

    //sending the token to the backend
            String backendApiUrlToGenerateSessionToken = your_url_with_cloud_code_to_generate_session_token;
            Log.i("URL: ", backendApiUrlToGenerateSessionToken);
    
            RequestQueue newRequestQueue = Volley.newRequestQueue(this);
    
            JSONObject getSessionTokenJsonRequestBody = new JSONObject();
            //back-end requires the token and Google client ID to be verified
            try {
                getSessionTokenJsonRequestBody.put("idToken", idTokenFromGoogle);
                getSessionTokenJsonRequestBody.put("GClientId", your_google_client_id);     //find it in google-services.json file
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
            final String requestBody = getSessionTokenJsonRequestBody.toString();
    
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, backendApiUrlToGenerateSessionToken, getSessionTokenJsonRequestBody,
                    new Response.Listener() {
                        @Override
                        public void onResponse(JSONObject response) {
                            //success callback
                            //set current user and continue
                            try {
                                ParseUser.becomeInBackground(response.getString("result"), new LogInCallback() {
                                    @Override
                                    public void done(ParseUser user, ParseException e) {
                                        if (user != null){
                                            //successfully logged in, take the user to the last page they were on
                                            finish();
                                        }else{
                                            //error
                                            Log.e("Login error: ", e.getMessage());
                                            //show error dialog, prompt user to login again
    
                                        }
                                    }
                                });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
    
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            //error callback
                            int  statusCode = error.networkResponse.statusCode;
                            NetworkResponse response = error.networkResponse;
    
                            Log.d("Error Response req: ","" + statusCode + " " + response.data.toString());
    
                        }
                    })
            {
                @Override
                public Map getHeaders(){
                    Map headers = new HashMap<>();
                    //post parameters
                    headers.put("X-Parse-Application-Id", getResources().getString(R.string.parse_app_id));
                    headers.put("X-Parse-REST-API-Key", getResources().getString(R.string.parse_rest_api_key));
                    headers.put("Content-Type", "application/json");
                    return headers;
                }
    
                @Override
                public byte[] getBody(){
                    try {
                        String body;
                        if (requestBody == null) body = null;
                        else body = String.valueOf(requestBody.getBytes("utf-8"));
                        return requestBody == null ? null : requestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        return null;
                    }
                }
            };
            newRequestQueue.add(jsonObjectRequest);
    

    (3): We've sent the token to our back-end, now we need to validate the token and generate our Parse session token in our back-end. Credit to @Amit http://pastebin.com/133LVYbm included logic to handle logged out users and subsequent login from the same Google account

    //generating a random password
    function getRandomString(){
        var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
        var string_length = 10;
        var randomstring = '';
    
        for (var i=0; i

    (4) Logic is in step (2) when the requestQueue returns the response we call the becomenbackground() method which saves the session and allows us to get the currently logged in user

提交回复
热议问题