【发布时间】:2017-04-24 00:19:28
【问题描述】:
我正在尝试在 Android Studio 中创建我的第一个 Android 应用,该应用将使用实时 API 数据。 要进行身份验证,我将遵循 Foursquare Dev 上的 OAuth 2.0 说明。 首先,这允许用户登录并授权应用程序。获得授权后,它会重定向到我服务器上http://example.com/apps/apptest/index.php 上的一个 php 文件,其中包含以下内容:
<?php
//INIT
header('Content-Type: application/json');
$response = array();
//AUTH
ini_set("allow_url_fopen", 1);
$clientId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$clientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$redirectUrl = urlencode("http://example.com/apps/apptest/index.php");
$code = htmlspecialchars($_GET["code"]);
$url = "https://foursquare.com/oauth2/access_token?client_id=";
$url .= $clientId;
$url .= "&client_secret=";
$url .= $clientSecret;
$url .= "&grant_type=authorization_code&redirect_uri=";
$url .= $redirectUrl;
$url .= "&code=";
$url .= $code;
$json = file_get_contents($url);
$obj = json_decode($json);
$access_token = $obj->access_token;
$url2 = "https://api.foursquare.com/v2/users/self?oauth_token=" . $access_token . "&v=" . date("Ymd");
$json2 = file_get_contents($url2);
$obj2 = json_decode($json2, true);
$userId = $obj2['response']['user']['id'];
// some mysql things I skipped in this code snippet
if (success) {
$response["success"] = 1;
$response["message"] = $access_token;
}
else {
$response["success"] = 0;
$response["message"] = "ERROR";
}
echo json_encode($response);
?>
现在的问题是:我无法从我的网页返回到我的安卓应用程序! 我想要的是以某种方式提供给应用程序的用户 ID 和/或令牌,以便我可以使用foursquare API! 我整天都在努力解决这个问题; - 起初我试图在 php 文件中放置一个 html 链接重定向回应用程序,但没有用 - 然后我尝试在 Android 中使用异步任务“使用”这个自写的“API”,但效果不佳。
现在我没有办法了,但我不想停止这个项目,有没有人可以告诉我下一步我可以尝试什么?
提前致谢!
【问题讨论】:
-
您是通过 webview 进行授权吗?
-
我正在使用此代码进行授权:
Uri uri = Uri.parse("https://foursquare.com/oauth2/authenticate?client_id=XXXXXXXX&response_type=code&redirect_uri=XXXXXXXX"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); -
它没有回答您的问题,但是您是否查看过适用于 Android 的 Foursquare Oauth 库?我相信它会为您处理所有这些github.com/foursquare/foursquare-android-oauth
-
这个库对我来说很迂回......
标签: php android api android-asynctask foursquare