简体   繁体   中英

How can I go back from a webpage to an Android app?

I'm trying to create my first Android app in Android Studio that will consume live API data. To authenticate, I'm following the OAuth 2.0 explanation on Foursquare Dev . First, this lets users login and authorize the app. When authorized, it redirects to a php file on my server on http://example.com/apps/apptest/index.php containing the following:

<?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);

?>

The problem now is: I can't get back from my web page to my android app! What I want is the user ID and/or token to be given to the app in some way so that I can consume the foursquare API! I've been trying all day to get this fixed; -At first I tried to put an html link in the php file redirecting back to the app, didn't work -Then I tried "consuming" this self written "API" with Asynchronous tasks in Android, didn't work as well.

Now I ran out of options but I don't want to stop this project, so is there anybody who could tell me what I could try next?

Thanks in advance!

I think deeplinking could solve your problem.

Take a look at https://developer.android.com/training/app-indexing/deep-linking.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM