0

こんにちは私は私が主な活動をするアンドロイドプロジェクトを作成しました

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String ss="https://accounts.google.com/o/oauth2/auth?client_id=anonymous&redirect_uri=https://cloudspokeproject.appspot.com/oauth2callback&response_type=code&scope=https://www.googleapis.com/auth/apps.groups.settings  https://apps-apis.google.com/a/feeds/groups/";

            Uri url=Uri.parse(ss);
            Intent in=new Intent(Intent.ACTION_VIEW,url);
            startActivity(in);


    }

次に、コードを使用して応答アクティビティを作成しました

public class Response extends Activity {

@Override
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        TextView text=new TextView(this);
    Uri uri=this.getIntent().getData();
    String code=uri.getQueryParameter("code");
    URL url;
    try {
        url = new URL("https://accounts.google.com/o/oauth2/token");

         String param="code="+code+"&client_id=anonymous&client_secret=anonymous&redirect_uri=https://cloudspokeproject.appspot.com/oauth2callback&grant_type=authorization_code";

         HttpURLConnection connection = 
    (HttpURLConnection)url.openConnection(); 
         connection.setDoOutput(true); 
         connection.setRequestMethod("POST"); 
         connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
         connection.getOutputStream().write( param.getBytes() ); 
      InputStream str= connection.getInputStream();
      BufferedReader reader=new BufferedReader(new InputStreamReader(str));
    String l="";
    String l1="";
    while((l=reader.readLine())!=null){
        l1+=l;
    }
        text.setText(l1);
        setContentView(text);

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    }}

これが私の最も重要なファイルコードです

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
 <activity
     android:name=".Response"
     android:label="Hello World"
     android:exported="false">
     <intent-filter>
          <action android:name="android.intent.action.VIEW"/> 
          <category android:name="android.intent.category.BROWSABLE"/> 
          <category android:name="android.intent.category.DEFAULT"/> 
          <data android:host="cloudspokeproject.appspot.com/oauth2callback" android:scheme="https"/> 
          </intent-filter>
 </activity>


</application>

アプリにGoogle認証を実装しようとしていますが、メインアクティビティはGoogleログインを開いており、サインインするとclouspokeproject.appspot.com/oauth2callbackにリダイレクトされますが、menifestのResponseアクティビティにインテントフィルターを配置すると、urlcloudspokeprojectになります。 apspot.com/oauth2callbackがブラウザで呼び出された場合、AndroidはそれをアクティビティResponse.iにリダイレクトする必要があります。Twitterおよび他のいくつかのAPIにoauthを正常に実装しましたが、今回はResponseアクティビティにリダイレクトされないため、どこが間違っているかを教えてください。間違いはごくわずかですが、訂正できるように私を指さしてください。

4

2 に答える 2

0

こんにちは、アクティビティ インテント フィルターに戻ってきて、次のようになります。

<intent-filter>
          <action android:name="android.intent.action.VIEW"/> 
          <category android:name="android.intent.category.BROWSABLE"/> 
          <category android:name="android.intent.category.DEFAULT"/> 
          <data android:host="cloudspokeproject.appspot.com" android:scheme="https"/> 
          </intent-filter>

ホスト属性の /oauth2callback を削除する必要があります。これは非常にまれな URL であり、プロジェクトで 1 回だけ使用されるため、現在は正常に機能しています。

于 2012-07-24T03:09:12.623 に答える
0

Responseアクティビティを呼び出さないようにブラウザを開くインテントを起動しています

于 2012-07-20T05:04:58.493 に答える