【问题标题】:foursquare integration in androidandroid中的foursquare集成
【发布时间】:2011-12-12 07:42:29
【问题描述】:

我想在 android 中集成foursquare。所以我尝试了两个例子。

第一个例子:

public class ActivityWebView extends Activity 
{
private static final String TAG = "ActivityWebView";

/**
 * Get these values after registering your oauth app at: https://foursquare.com/oauth/
 */

public static final String CLIENT_ID = "";
public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-foursquare";
public static final String OAUTH_CALLBACK_HOST = "callback";
public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);

    String url =
        "https://foursquare.com/oauth2/authenticate" + 
            "?client_id=" + CLIENT_ID + 
            "&response_type=token" + 
            "&redirect_uri=" + CALLBACK_URL;

    // If authentication works, we'll get redirected to a url with a pattern like:  
    //
    //    http://YOUR_REGISTERED_REDIRECT_URI/#access_token=ACCESS_TOKEN
    //
    // We can override onPageStarted() in the web client and grab the token out.
    WebView webview = (WebView)findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            String fragment = "#access_token=";
            int start = url.indexOf(fragment);
            if (start > -1) {
                // You can use the accessToken for api calls now.
                String accessToken = url.substring(start + fragment.length(), url.length());

                Log.v(TAG, "OAuth complete, token: [" + accessToken + "].");

                Toast.makeText(ActivityWebView.this, "Token: " + accessToken, Toast.LENGTH_SHORT).show();
            }
        }
    });
    webview.loadUrl(url);
}

}

第二个例子:

http://blog.doityourselfandroid.com/2011/09/05/integrate-foursquare-android-application/

我在 Manifest 文件中添加了额外的代码

<activity android:name=".ActivityWebView" android:launchMode="singleTask"  android:noHistory="true"> 
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="x-oauthflow-foursquare" android:host="callback" />
        </intent-filter>
    </activity>

运行这两个应用程序后,身份验证成功,但显示带有此消息的 web 视图

you dont have permission to open this page x-oauthflow-foursquare://callback#access_token=DRWDP004zzAIZ1PPJEANEBFCM3NZJ1T414U2Z

所以请告诉我它是否需要添加任何额外的权限。请解决我的问题。

【问题讨论】:

    标签: android integration foursquare


    【解决方案1】:

    使用 Foursquare API 检查验证部分和回调部分的代码:

    public class ActivityMain extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(ActivityMain.this, ActivityWebView.class);
                startActivity(intent);
            }
        });
    }
    }
    

    ActivityWebView.Java:点击按钮后:

    public class ActivityWebView extends Activity{
        private static final String TAG = "ActivityWebView";
        public static final String CLIENT_ID = "01QWBUR2UUFSMMLZXOM5YY0UWCM4BC0F2IOZDJ0S5XGOINQM";
        public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-foursquare";
        public static final String OAUTH_CALLBACK_HOST = "callback";
        public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);       
    
        String url =
            "https://foursquare.com/oauth2/authenticate" + 
                "?client_id=" + CLIENT_ID + 
                "&response_type=token" + 
                "&redirect_uri=" + CALLBACK_URL;
    
                WebView webview = (WebView)findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                String fragment = "#access_token=";
                int start = url.indexOf(fragment);
                if (start > -1) {
                    // You can use the accessToken for api calls now.
                    String accessToken = url.substring(start + fragment.length(), url.length());
    
                    Log.v(TAG, "OAuth complete, token: [" + accessToken + "].");
    
                    Toast.makeText(ActivityWebView.this, "Token: " + accessToken, Toast.LENGTH_SHORT).show();
                    editor.putString("Access_Token", accessToken);
                    editor.commit();
                    startActivity(new Intent(ActivityWebView.this,Nearest_Places_View.class));  // After Successfull Login the Web view will Redirected to Nearest_Places_View Activity
                }
            }
        });
        webview.loadUrl(url);
    }
    }
    

    activity_webview.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
     <WebView  
        android:id="@+id/webview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"  />
     </LinearLayout>
    

    回调后的活动:

    public class Nearest_Places_View extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.anylayout);
    
            // Do your All stuff for Foursquare for example getting User Checkins,Nearest Place
        }
     }
    

    您的清单文件应如下所示:

    <?xml version="1.0" encoding="utf-8"?>
     <manifest 
      xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.foursquare.android.oauth"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-sdk android:minSdkVersion="4" />
       <application     android:icon="@drawable/icon"    android:label="@string/app_name">
            <activity   android:name=".ActivityMain"        android:label="@string/app_name">
               <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           </activity>
    
      <activity
      android:name=".Nearest_Places_View"
      />
    
    <activity android:name=".ActivityWebView" android:launchMode="singleTask">>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="x-oauthflow-foursquare" android:host="callback" />
        </intent-filter>
    </activity>
    

    所有三个 1. Activity 2.manifest 3.Settings 页面的回调应该相同。那么只有它会起作用。

    【讨论】:

    • 尝试使用您的代码,它的抛出授权失败问题。如何编写重定向 url,例如我有 abcd.com 是否正确?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2014-10-21
    • 2016-02-21
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多