2016-12-08 13 views
1

Androidのスタジオで初めてのAndroidアプリを作成しようとしていますが、これはライブAPIデータを消費します。 認証するには、Foursquare Devに関するOAuth 2.0の説明に従っています。 まず、これを使用すると、ユーザーはログインしてアプリケーションを認証できます。許可すると、それは以下を含むhttp://example.com/apps/apptest/index.php上の私のサーバー上のPHPファイルにリダイレクト:ウェブページからAndroidアプリに戻るにはどうすればよいですか?

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

?> 

問題が今ある:私は私のAndroidアプリに自分のWebページから取り戻すことはできません! 私が望むのは、ユーザーIDやトークンを何らかの形でアプリに与えて、私がfoursquare APIを使うことができるようにすることです! 私はこの問題を解決するために一日中努力してきました。 - 最初は、アプリケーションにリダイレクトされたPHPファイルにhtmlリンクを挿入しようとしましたが、動作しませんでした。 - 私はAndroidで非同期タスクを使ってこの自己書き出し "API"を "消費"しようとしました。よく

私はこのオプションを使い果たしましたが、私はこのプロジェクトをやめたくないので、次に試してみたいことを教えてくれる人はいますか?

ありがとうございます!

+0

あなたはwebview経由で承認していますか? –

+0

私はこのコードで承認をしています: 'Uri uri = Uri.parse(" https://foursquare.com/oauth2/authenticate?client_id=XXXXXXXX&response_type=code&redirect_uri=XXXXXXXX "); 意図インテント=新しいインテント(Intent.ACTION_VIEW、uri); startActivity(intent); ' – ernie379

+1

あなたの質問には答えませんが、Android用のFoursquare Oauthライブラリを見ましたか?私はそれがあなたのためにすべてを処理すると信じていますhttps://github.com/foursquare/foursquare-android-oauth –

答えて

関連する問題