如何在Android App中实现基于令牌的Rest API

时间:2018-08-21 06:58:45

标签: java android api oauth-2.0 access-token

我想在我的应用程序中使用基于Http Restful令牌的api,但是当我在邮递员中发送 ACCESS KEY,SECRET KEY,APP_ID,DEVICE_ID 时,它会给我数据。我很困惑,这是不是oAuth2。如何在Android应用中实现此API。我在youtube上观看了很多有关oAuth 2和基于令牌的api的视频,但不适用于我。当我们发送请求时,JSON就是这样。

{"status":true,"version":"2.0.2","status_code":200,"expires":"-1",
"auth":{"access_token":"2s153415154217785s1031898106557579455","expires":"1534858655.0"},
"Etag":null,"cache_key":"auth|2s153415154217785s1031898106557579455|http://192.167.41.180:4000/|"}

,以及当我们发送带有所需api链接的令牌时 https://rest.cricketapi.com/rest/v2/kabaddi/season/pkl_2017/recent_matches/?access_token=2s153415154217785s1031898106557579455

它像这样显示

{
    "Etag": "1534772423.0",
    "cache_key": "kabaddi|recent_matches|pkl_2017|micro_card",
    "data": {
        "card_type": "micro_card",
        "cards": [
            {
                "cache_key": "kabaddi|match|pkl_2017_final|micro_card",
                "expires": "1534772423.0",
                "group": {
                    "key": "pkl_2017_final",
                    "name": "Final"
                },
                "key": "pkl_2017_final",
                "name": "GUJARAT FORTUNEGIANTS vs PATNA PIRATES",
                "result": {
                    "str": "PATNA PIRATES won the match",
                    "winner_team": "b"
                },
                "round": {
                    "key": "pkl_2017_final",
                    "name": "Final"
                },
                "season": {
                    "competition": {
                        "key": "pkl_2017",
                        "name": "Pro Kabaddi 2017"
                    },
                    "key": "pkl_2017",
                    "name": "Pro Kabaddi 2017",
                    "short_name": "PK 17"
                },
                "start_date": {
                    "iso": "2017-10-28T14:30+00:00",
                    "str": "28th Oct 2017 14:30 GMT",
                    "timestamp": "1509201000"
                },
                "status": "completed",
                "sub_card_name": "Final",
                "teams": {
                    "a": {
                        "key": "guj_fort",
                        "name": "GUJARAT FORTUNEGIANTS",
                        "short_name": "GF"
                    },
                    "b": {
                        "key": "pat_pirates",
                        "name": "PATNA PIRATES",
                        "short_name": "PP"
                    }
                },
                "venue": {
                    "name": "Chennai"
                }
            },
            {
                "cache_key": "kabaddi|match|pkl_2017_e2|micro_card",
                "expires": "1534772424.0",
                "group": {
                    "key": "pkl_2017_e",
                    "name": "Eliminator"
                },
                "key": "pkl_2017_e2",
                "name": "BENGAL WARRIORS vs PATNA PIRATES",
                "result": {
                    "str": "PATNA PIRATES won the match",
                    "winner_team": "b"
                },
                "round": {
                    "key": "pkl_2017_e",
                    "name": "Eliminator"
                },
                "season": {
                    "competition": {
                        "key": "pkl_2017",
                        "name": "Pro Kabaddi 2017"
                    },
                    "key": "pkl_2017",
                    "name": "Pro Kabaddi 2017",
                    "short_name": "PK 17"
                },
                "start_date": {
                    "iso": "2017-10-26T14:30+00:00",
                    "str": "26th Oct 2017 14:30 GMT",
                    "timestamp": "1509028200"
                },
                "status": "completed",
                "sub_card_name": "Qualifier 2",
                "teams": {
                    "a": {
                        "key": "ben_warriors",
                        "name": "BENGAL WARRIORS",
                        "short_name": "BW"
                    },
                    "b": {
                        "key": "pat_pirates",
                        "name": "PATNA PIRATES",
                        "short_name": "PP"
                    }
                },
                "venue": {
                    "name": "Chennai"
                }
            },
            {
                "cache_key": "kabaddi|match|pkl_2017_e1|micro_card",
                "expires": "1534772424.0",
                "group": {
                    "key": "pkl_2017_e",
                    "name": "Eliminator"
                },
                "key": "pkl_2017_e1",
                "name": "PUNERI PALTAN vs PATNA PIRATES",
                "result": {
                    "str": "PATNA PIRATES won the match",
                    "winner_team": "b"
                },
                "round": {
                    "key": "pkl_2017_e",
                    "name": "Eliminator"
                },
                "season": {
                    "competition": {
                        "key": "pkl_2017",
                        "name": "Pro Kabaddi 2017"
                    },
                    "key": "pkl_2017",
                    "name": "Pro Kabaddi 2017",
                    "short_name": "PK 17"
                },
                "start_date": {
                    "iso": "2017-10-24T15:30+00:00",
                    "str": "24th Oct 2017 15:30 GMT",
                    "timestamp": "1508859000"
                },
                "status": "completed",
                "sub_card_name": "Eliminator 3",
                "teams": {
                    "a": {
                        "key": "pune_paltan",
                        "name": "PUNERI PALTAN",
                        "short_name": "PUP"
                    },
                    "b": {
                        "key": "pat_pirates",
                        "name": "PATNA PIRATES",
                        "short_name": "PP"
                    }
                },
                "venue": {
                    "name": "Mumbai"
                }
            },
            {
                "cache_key": "kabaddi|match|pkl_2017_q3|micro_card",
                "expires": "1534772424.0",
                "group": {
                    "key": "pkl_2017_q",
                    "name": "Qualifier"
                },
                "key": "pkl_2017_q3",
                "name": "GUJARAT FORTUNEGIANTS vs BENGAL WARRIORS",
                "result": {
                    "str": "GUJARAT FORTUNEGIANTS won the match",
                    "winner_team": "a"
                },
                "round": {
                    "key": "pkl_2017_q",
                    "name": "Qualifier"
                },
                "season": {
                    "competition": {
                        "key": "pkl_2017",
                        "name": "Pro Kabaddi 2017"
                    },
                    "key": "pkl_2017",
                    "name": "Pro Kabaddi 2017",
                    "short_name": "PK 17"
                },
                "start_date": {
                    "iso": "2017-10-24T14:30+00:00",
                    "str": "24th Oct 2017 14:30 GMT",
                    "timestamp": "1508855400"
                },
                "status": "completed",
                "sub_card_name": "Qualifier 1",
                "teams": {
                    "a": {
                        "key": "guj_fort",
                        "name": "GUJARAT FORTUNEGIANTS",
                        "short_name": "GF"
                    },
                    "b": {
                        "key": "ben_warriors",
                        "name": "BENGAL WARRIORS",
                        "short_name": "BW"
                    }
                },
                "venue": {
                    "name": "Mumbai"
                }
            },
            {
                "cache_key": "kabaddi|match|pkl_2017_q2|micro_card",
                "expires": "1534772424.0",
                "group": {
                    "key": "pkl_2017_q",
                    "name": "Qualifier"
                },
                "key": "pkl_2017_q2",
                "name": "PATNA PIRATES vs HARYANA STEELERS",
                "result": {
                    "str": "PATNA PIRATES won the match",
                    "winner_team": "a"
                },
                "round": {
                    "key": "pkl_2017_q",
                    "name": "Qualifier"
                },
                "season": {
                    "competition": {
                        "key": "pkl_2017",
                        "name": "Pro Kabaddi 2017"
                    },
                    "key": "pkl_2017",
                    "name": "Pro Kabaddi 2017",
                    "short_name": "PK 17"
                },
                "start_date": {
                    "iso": "2017-10-23T15:30+00:00",
                    "str": "23rd Oct 2017 15:30 GMT",
                    "timestamp": "1508772600"
                },
                "status": "completed",
                "sub_card_name": "Eliminator 2",
                "teams": {
                    "a": {
                        "key": "pat_pirates",
                        "name": "PATNA PIRATES",
                        "short_name": "PP"
                    },
                    "b": {
                        "key": "har_steelers",
                        "name": "HARYANA STEELERS",
                        "short_name": "HS"
                    }
                },
                "venue": {
                    "name": "Mumbai"
                }
            },
            {
                "cache_key": "kabaddi|match|pkl_2017_q1|micro_card",
                "expires": "1534772424.0",
                "group": {
                    "key": "pkl_2017_q",
                    "name": "Qualifier"
                },
                "key": "pkl_2017_q1",
                "name": "PUNERI PALTAN vs U.P. YODDHA",
                "result": {
                    "str": "PUNERI PALTAN won the match",
                    "winner_team": "a"
                },
                "round": {
                    "key": "pkl_2017_q",
                    "name": "Qualifier"
                },
                "season": {
                    "competition": {
                        "key": "pkl_2017",
                        "name": "Pro Kabaddi 2017"
                    },
                    "key": "pkl_2017",
                    "name": "Pro Kabaddi 2017",
                    "short_name": "PK 17"
                },
                "start_date": {
                    "iso": "2017-10-23T14:30+00:00",
                    "str": "23rd Oct 2017 14:30 GMT",
                    "timestamp": "1508769000"
                },
                "status": "completed",
                "sub_card_name": "Eliminator 1",
                "teams": {
                    "a": {
                        "key": "pune_paltan",
                        "name": "PUNERI PALTAN",
                        "short_name": "PUP"
                    },
                    "b": {
                        "key": "up_yoddha",
                        "name": "U.P. YODDHA",
                        "short_name": "UPY"
                    }
                },
                "venue": {
                    "name": "Mumbai"
                }
            }
        ]
    },
    "expires": "1534772423.0",
    "status": true,
    "status_code": 200,
    "version": "2.0.3"
}

我想在我的Android应用程序中实现。但是使用基于改造令牌的身份验证和oAuth 2后,它无法在android应用中获取数据。

感谢您的阅读。

1 个答案:

答案 0 :(得分:0)

目前,我正在应用程序中使用Rest Api,此技术已在服务器上使用,您需要使用应用程序中的库与api通信。对于我的开发,我使用了Retrofit,在我看来,这是用于在Android上使用Rest Api的最灵活的库。因此,有一个像Volley这样的图书馆。底线很简单,您必须将密钥发送到服务器以访问所需的信息,并且为了发送某些内容,您需要获取此密钥。服务器部分的开发人员应向您提供文档。也许我的回答会对您有所帮助。